Understanding React component's lifecycle methods
Think about what a React component does? It describes what to render. We know that it uses the render()
method for this. However, sometimes, having only the render()
method is not enough because what if we want to do something before or after the component has rendered? What if we want to be able to decide whether a component's render()
method should be called at all?
Looks like what we're describing is a process during which the React component is rendered. This process has various stages, for example, before render, render, and after render. In React, this process is called the component's lifecycle. Each React component goes through this process. What we want is a way to hook into that process, and call our own functions at different stages of that process in order to have a greater control over it. For this purpose, React provides a number of methods that we can use to get notified when a certain stage in a component...