Found 217 Articles for Javascript Library

Promises Callbacks And Async/Await

Shyam Hande
Updated on 04-Sep-2019 14:03:41

695 Views

First we have to understand two main conceptsSynchronous programmingAsynchronous programmingSYNCHRONOUS PROGRAMMINGIt waits for each statement to complete its execution before going to next statement.This approach can slow down application process if statements are not dependent on each other but still they are waiting for execution as they are in queue.ASYNCHRONOUS PROGRAMMINGIt does not wait for current statement to complete its execution before moving to next statement. e.g. calling a web service and executing file copy in JavaScript.The call to web service can take some time to return a result meanwhile we can complete some other actions.Once server provides the result, ... Read More

Styling in React.js

Shyam Hande
Updated on 04-Sep-2019 13:50:26

472 Views

Styling in React.js can be done in below two wayscss style sheetsinline styleLet’s see CSS style sheets firstWe have App.js file as shown below −import React, {Component} from 'react'; import './App.css'; class App extends Component {    render(){       return (                       Styling React Components                   );    } } export default App;In App.js file we have imported an App.css file which contains css class myColoredTextPlease noteWe used name of css class in double quotes with attribute classNameJSX uses ... Read More

Standalone React.js basic example

Shyam Hande
Updated on 03-Jul-2020 09:02:34

2K+ Views

Lets first start with writing a simple HTML code and see how we can use ReactBasic React example  − Create a simple div like below −    Steve    My hobby: Cricket Add some styling elements.player{    border:1px solid #eee;    width:200px;    box-shadow:0 2px 2px #ccc;    padding: 22px;    display:inline-block;    margin:10px; }This is just like normal html data in web app. Now, we may have multiple same players and we then have to replicate the same div like below    David    My hobby: Cricket These div are same in structure but having different data inside. Here, ... Read More

Returning adjacent element in React.js and HOC

Shyam Hande
Updated on 04-Sep-2019 13:16:01

213 Views

Generally the return statement in React’s render method returns a single div element enclosing all the child jsx like below −render(){    return (                Header          Test          ); }Here we cannot simply return multiple elements, we need to have a parent container similar to div as shown above.If no parent container element, then we can even return an array like below −While returning an array we will require an unique key to be given to each element of an arrayrender(){    return (   ... Read More

React.js stateless vs stateful

Shyam Hande
Updated on 04-Sep-2019 14:49:37

4K+ Views

Understand the difference in stateless and stateful react componentsIn state management post, we learned how we can handle state in react if something is changed.Stateless components are simple functional component without having a local state but remember there is a hook in react to add state behavior in functional component as well.Stateful component can contains the state object and event handling function, user actions as well.Stateless component are pure in nature which does a very specific task.import React from 'react'; const player = (props) => {    return (                I'm a Player: ... Read More

React.js routing

Shyam Hande
Updated on 04-Sep-2019 13:00:52

2K+ Views

Before React Router v4, the routing in react was static but after v4 update its dynamic. In single page applications, we have only one page index.html and different components which gets displayed based on routes.In non spa web applications, server returns a page on request. To start with routing , first install the routing package −npm install –save react-router-domOnce we create the create the project with create-react-app, we will see there is only one html file and that is index.html and a single component named as AppNow, we will create two more components AboutUs.jsx and ContactUs.jsxAboutUs.jsximport React from 'react' class ... Read More

React.js component lifecycle error handling phase

Shyam Hande
Updated on 04-Sep-2019 12:56:01

621 Views

There are two main methods in error handling. These method are used in the error boundary mechanism in React. We wrap the component ( from where there is a possibility of error occurrences ) in a class which handles the below methods.Static method getDerivedStateFromError(error)componentDidCatch(error, info)Static getDerivedStateFromError(error): As name of the method suggest, we can update state object here based on error received from descended component.componentDidCatch(error, info): we can log the errors using api call. This is helpful in displaying useful error message on screen instead of technical errors.A class can be termed as an error boundary if it implements at ... Read More

React.js Component Lifecycle - Updating phase

Shyam Hande
Updated on 04-Sep-2019 12:03:46

228 Views

The update lifecycle can be triggered by two events −Update of props by parent componentChange in local stateUpdate of props by parent componentcomponentWillReceiveProps(nextProps) −This is the first method which gets called on prop change. It contains one argument nextProps means the newly changed props.Here, we can synchronize local state with props if requiredIf state is not needed to synchronize with props then probably no need to implement this method.Causing side effects should be avoided here. Because if side effects used here, it may results into re-render or performance issues.Now, componentWillReceiveProps is deprecated and replaced by static method getDerivedStateFromProps(props, state). As ... Read More

React.js Component Lifecycle - Unmounting

Shyam Hande
Updated on 04-Sep-2019 11:52:05

283 Views

ComponentWillUnmount is the only method that executes in unmount phase.Component enters into this phase when there is no matching in element tree for this component.Just before the component gets removed from actual DOM, this method gets called.Along with removal of this component from DOM tree, all children of this component also gets removed automatically.Once component is removed from the DOM, it gets available for the garbage collection in React.Cleanup activities can be done in this method. E.g. clear localstorage variables used in app, clear session, clean up charts, cleanup timers, cancel pending api requests etc.componentWillUnmount(){    this.resetSession(); //example method to ... Read More

React.js Component Lifecycle - Mounting phase

Shyam Hande
Updated on 04-Sep-2019 11:43:13

238 Views

Each stateful means class based component goes through four types of lifecycle phases.Mounting or CreationUpdatingUnmounting or DestroyError handling phaseReact.js Component Lifecycle - Mounting phaseThe methods that executes during creation lifecycle are −constructorcomponentWillMount (Only available till version 17 )rendercomponentDidMountconstructor ()It’s an ES6 feature and not a React provided method.React uses the constructor to pass the props to parent Component extended from React library.constructor(props){    super( props ); }Passing of props to parent Component helps us to use this.props in the component.We can initialize the state for the component in constructor. Important point is we should not use setState method in constructor. ... Read More

Advertisements