I just couldn't find the practical usage example of these two life cycle method. I've been writing in react for a while, but componentDidMount just get the job done, it means to call fetch async data, but I don't see the point of willMount, any clue?
The componentWillMount() lifecycle hook is primarily used to implement server-side logic before the actual rendering happens, such as making an API call to the server. In this guide, you will learn to use componentWillMount() and make API calls after the initial component rendering.
The reason for this decision is twofold: All three methods are frequently use incorrectly and there are better alternatives. When asynchronous rendering is implemented in React, misuse of these will be problematic, and the interrupting behavior of error handling could result in memory leaks.
Calling API in constructor() or componentWillMount() is not a syntax error but increases code complexity and hampers performance. So, to avoid unnecessary re-rendering and code complexity, it's better to call API after render(), i.e componentDidMount().
The equivalent of componentDidMount in hooks is the useEffect function. Functions passed to useEffect are executed on every component rendering—unless you pass a second argument to it.
Comparing the two:
componentWillMount
runs before initial rendering. But it is not advised to do any subscriptions and state setting in this method. If you want to some of that before rendering you can use the constructor of the component.
componentWillMount()
is invoked immediately before mounting occurs. It
is called beforerender()
, therefore callingsetState()
synchronously
in this method will not trigger an extra rendering. Generally, we
recommend using the constructor() instead. Avoid introducing any
side-effects or subscriptions in this method. For those use cases, use
componentDidMount() instead.
Possible use cases:
componentDidMount
however runs after the initial rendering and marks when the Component has finally finished mounting the DOM. So you can use this to set up subscriptions and listeners and even fetch data to setState.
componentDidMount()
is invoked immediately after a component is
mounted. Initialization that requires DOM nodes should go here. If you
need to load data from a remote endpoint, this is a good place to
instantiate the network request. This method is a good place to set up
any subscriptions. If you do that, don’t forget to unsubscribe incomponentWillUnmount()
. CallingsetState()
in this method will trigger
an extra rendering, but it will happen before the browser updates the
screen. This guarantees that even though therender()
will be called
twice in this case, the user won’t see the intermediate state. Use
this pattern with caution because it often causes performance issues.
It can, however, be necessary for cases like modals and tooltips when
you need to measure a DOM node before rendering something that depends
on its size or position.
Possible use cases:
BIG DIFFERENCE: In Server Side Rendering only componentWillMount
runs in the server side. So if you're ever using SSR make sure you don't have any server side code in componentDidMount
Currently you can (you decide if you should) use both to setup initial state. Generally I've seen most people use componentDidMount
for this but requirements change and you might find some use case for using componentWillMount
.
There has been however talks about deprecating the lifecycle method. here
Difference between componentWillMount and componentDidMount is in terms of when they are called. componentWillMount is called before render which means setting state synchronously in componentWillMount will not cause extra render call while componentDidMount is called after a render and hence any state change in this method will cause one extra render call.
For any async call componentDidMount is the one to be used instead of componentWillMount.
componentWillMount is deprecated as of React 16.3 (March 2018). Until React 17, this method will continue to work. In place of it, you can use the constructor in a class component OR componentDidMount.
From the official docs:
UNSAFE_componentWillMount() was previously named componentWillMount. That name will continue to work until version 17. Use the rename-unsafe-lifecycles codemod to automatically update your components.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With