componentWillReceiveProps and other lifecycle methods seems like deceptive temptation to bring unnecessary complexity and noise to the code in the hands of inexperienced React coder. Why do they exist? What are their most typical use cases? In the moment of uncertainty, how would I know if the answer lies in the lifecycle methods?
I have been using react for couple of months now, and most of my work is creating a large application from scratch. So the same questions have presented themselves in the start.
The following information is based on learning while development and going through multiple docs out there to get it right.
As asked in the question here are couple of uses cases for the lifecycle methods in react
stateinitialisations which are based on the props passed by parents.
this.setState()is not allowed in this function , it is to be done in componentWillReceiveProps or componentDidUpdate
In the moment of uncertainty, how would I know if the answer lies in the lifecycle methods?
What analogy i suggest
Change is triggered in the component itself
Change is triggered outside of the component
Here are some more scenarios -
Does the change in state/props requires the DOM to be modified?
Does the change in state/props requires to data to be updated?
Props being passed to a child are changed , child needs to update
Adding an event listener