We’ve seen that React components can be highly dynamic. They get created, rendered, added to the DOM, updated, and removed. All of these steps are part of a component’s lifecycle.
The component lifecycle has three high-level parts:
- Mounting, when the component is being initialized and put into the DOM for the first time
- Updating, when the component updates as a result of changed state or changed props
- Unmounting, when the component is being removed from the DOM
Every React component you’ve ever interacted with does the first step at a minimum. If a component never mounted, you’d never see it!
Most interesting components are updated at some point. A purely static component—like, for example, a logo—might not ever update. But if a component’s state changes, it updates. Or if different props are passed to a component, it updates.
Finally, a component is unmounted when it’s removed from the DOM. For example, if you have a button that hides a component, chances are that component will be unmounted. If your app has multiple screens, it’s likely that each screen (and all of its child components) will be unmounted. If a component is “alive” for the entire lifetime of your app (say, a top-level
<App /> component or a persistent navigation bar), it won’t be unmounted. But most components can get unmounted one way or another!
It’s worth noting that each component instance has its own lifecycle. For example, if you have 3 buttons on a page, then there are 3 component instances, each with its own lifecycle. However, once a component instance is unmounted, that’s it—it will never be re-mounted, or updated again, or unmounted.
Take a look at this helpful reference diagram. Don’t worry—you don’t have to understand all of it right now—but give it a look.
Which methods are called during the mounting phase? What about the update phase?
What causes an update to occur?
Again, it’s okay if you don’t know the answers to these yet. We’ll learn all about that soon!