Why components need a life cycle
React components go through a life cycle. In fact, the render()
method that you've implemented in your components so far in this book is actually a life cycle method. Rendering is just one life cycle event in a React component.
For example, there are life cycle events for when the component is mounted to the DOM, when the component is updated, and so on. Life cycle events are yet another moving part, so you'll want to keep them to a minimum. As you'll learn in this chapter, some components do need to respond to life cycle events to perform initialization, render heuristics, clean up after the component when it's unmounted from the DOM, or handle errors thrown by the component.
The following diagram gives you an idea of how a component flows through its life cycle, calling the corresponding methods in turn:
These are the two...