react lifecycle methods understanding - javascript
The React lifecycle: methods and hooks explained
React Lifecycle Methods- how and when to use them
Very nice article! I would also add that componentDidUpdate is the place to be if you want to do an AJAX call in response to the props being changed (just like componentDidMount was the place to do initiall request, as long as you're not doing SSR).
One of the reasons to that, over using componentWillReceiveProps, is that with Fiber cWRP can be called multiple times before the component is, indeed, re-rendered, and each time you will get the same nextProps and this.props which cna lead to multiple requests being made.
My go to article to reference all of the React lifecycle methods
Videos
1) componentWillReceiveProps is called before componentWillUpdate in React's update lifecycle. You are right that componentWillReceiveProps allows you to call setState. On the other hand componentWillUpdate is a callback to use when you need to respond to a state change.
The fundamental difference between props and state is that state is private to the component. That's why neither a parent component or anybody else can manipulate the state (e.g. call setState) of the component. So the default workflow for the parent-child component relationship would be the following:
- Parent passes new props to the child
- Child handles new props in 'componentWillReceiveProps', calls
setStateif necessary - Child handles new state in 'componentWillUpdate' - but if your component is stateful, handling props in 'componentWillReceiveProps' will be enough.
2) You provided quite a good code example to illustrate the difference. Default values set in getInitialState will be used for initial rendering. The loadData call from componentWillMount will initiate an AJAX request which may or may not succeed - moreover it is unknown how long it will take to complete. By the time the AJAX request completes and setState is called with new state, the component will be rendered in the DOM with default values. That is why it makes total sense to provide default state in getInitialState.
Note: I found Understanding the React Component Lifecycle article a huge help for understanding React's lifecycle methods.
Four phases of a React component lifecycle
Initialization
Mounting
Update
Unmounting
Here's a quick walkthrough of the different methods of the lifeCycle of a component. You must have good understanding of the lifecycle methods to code efficiently in react.
Life Cycle Phase Methods
Methods in Mounting Phase:
This phase begins when an instance of a component is created and when it gets rendered into the DOM.
1.constructor(props) - it is called when the component is first initialized. This method is only called once.
2.componentWillMount() - it is called when a component is about to mount.
3.render() -it is called when a component is rendered.
4.componentDidMount() - it is called when a component has finished mounting.
Methods in Updating Phase:
This phase begins when a component's properties (a.k.a props) or state changes.
1.componentWillReceiveProps(nextProps) - it is called when a component has updated and is receiving new props.
2.shouldComponentUpdate(nextProps, nextState) - it is called after receiving props and is about to update. If this method returns false, componentWillUpdate(), render(), and componentDidUpdate() will not execute.
3.componentWillUpdate(nextProps, nextState) - it is called when a component is about to be updated.
4.render() - called when a component is rerendered.
5.componentDidUpdate(prevProps, prevState) - it is called when a component has finished updating.
Methods in Unmounting Phase:
This phase begins when a component is being removed from the DOM.
1.componentWillUnmount() - it is called immediately before a component unmounts.
Ref: https://hackernoon.com/reactjs-component-lifecycle-methods-a-deep-dive-38275d9d13c0
A React component undergoes three different phases in its lifecycle, including mounting, updating, and unmounting. Each phase has specific methods responsible for a particular stage in a component's lifecycle. These methods are technically particular to class-based components and not intended for functional components.
However, since the concept of Hooks was released in React, you can now use abstracted versions of these lifecycle methods when you’re working with functional component state. Simply put, React Hooks are functions that allow you to “hook into” a React state and the lifecycle features within function components.
In this post, you'll learn more about the React component lifecycle and the different methods within each phase (for class-based components), focusing particularly on methods and hooks.
https://retool.com/blog/the-react-lifecycle-methods-and-hooks-explained/