Current Version is 16.13.1
How Does ReactJS Work?
Reactjs create a virtual DOM, which makes ReactJs unique.Instead of manipulating the browser’s DOM directly, React creates a virtual DOM in memory, where it does all the necessary manipulating, before making the changes in the browser DOM.
ReactJS creates Virtual DOM and Exact Copy of Real DOM.
ReactJS only changes what we need to change. Then ReactJs checks with the actual copy of the Real Dom. Then it got to know there is a difference between Virtual and the copy exact of DOM, So ReactJS updates only that particular component with the Real DOM.
Advantages of Virtual DOM
- Updating the Virtual DOM in React.js always increases the performance.
- React’s Virtual DOM provides a more efficient way of updating the view in a web application. Each time the underlying data changes in a React app, a new Virtual DOM representation of the user interface is created. Rendering the Virtual DOM is always faster than rendering the UI in the actual browser DOM.
Let’s Quickly see the ReactJS LifeCycles
What is LifeCycle?
When our page is loaded into browser. There are some stages as intialization,rendered and destroyed.The three stages are:
Mounting, Updating, and Unmounting.
Mounting means Putting element into DOM.
These methods are called in the following order when an instance of a component is being created and inserted into the DOM:
(By clicking above methods, you can see more in reactjs.org)
Constructor is called before mounted.This can be used to bind event handlers to the component and/or to initialize the component’s local state. We need to call
because it will throw errors if you don’t call
super(props)before this.props. Never call setState() inside the constructor.
2. static getDerivedStateFromProps()
getDerivedStateFromProps is one of those newly introduced lifecycle method replacing
render() method is required, and is the method that actual outputs HTML to the DOM.
After render, componentDidMount() is called.This is where you run statements that requires that the component is already placed in the DOM.
Whenever there is change in state or props, component needed to be updated. That phase is called as Updating. In here we have,
As in mounting, in updatation also we call getDerivedStateFromProps().The first method called after we updated.This is a natural place to set the state object based on intial props.
shouldComponentUpdate() method you can return a Boolean value that specifies whether React should continue with the rendering or not. Defaultly value is True
Render is called when update completes.
Access the state and props before the update,meaning that even after the update, you can check the values before update
componentDidUpdate method is called after the component is updated in the DOM.
The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it.
React has only one built-in method that gets called when a component is unmounted:
This method is called when a component is being removed from the DOM: