1.1. React lifecycle methods #
Created Monday 30 May 2022
FIXME: copied as of now, to be re-written if needed.
React life-cycle hooks will have the methods that will be automatically called at different phases in the component life-cycle and thus it provides good control over what happens at the invoked point. It provides the power to effectively control and manipulate what goes on throughout the component life-cycle.
For example, if you are developing the YouTube application, then the application will make use of a network for buffering the videos and it consumes the power of the battery (assume only these two). After playing the video if the user switches to any other application, then you should make sure that the resources like network and battery are being used most efficiently. You can stop or pause the video buffering which in turn stops the battery and network usage when the user switches to another application after video play.
So we can say that the developer will be able to produce a quality application with the help of life-cycle methods and it also helps developers to make sure to plan what and how to do it at different points of birth, growth, or death of user interfaces.
The various life-cycle methods are:
constructor()
: This method will be called when the component is initiated before anything has been done. It helps to set up the initial state and initial values.getDerivedStateFromProps()
: This method will be called just before element(s) rendering in the DOM. It helps to set up the state object depending on the initial props. The getDerivedStateFromProps() method will have a state as an argument and it returns an object that made changes to the state. This will be the first method to be called on an updating of a component.render()
: This method will output or re-render the HTML to the DOM with new changes. The render() method is an essential method and will be called always while the remaining methods are optional and will be called only if they are defined.componentDidMount()
: This method will be called after the rendering of the component. Using this method, you can run statements that need the component to be already kept in the DOM.shouldComponentUpdate()
: The Boolean value will be returned by this method which will specify whether React should proceed further with the rendering or not. The default value for this method will be True.getSnapshotBeforeUpdate()
: This method will provide access for the props as well as for the state before the update. It is possible to check the previously present value before the update, even after the update.componentDidUpdate()
: This method will be called after the component has been updated in the DOM.componentWillUnmount()
: This method will be called when the component removal from the DOM is about to happen.