Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 132 ยท Hooks

Component Lifecycle with Hooks

See how a function component mounts, updates and unmounts - render versus commit versus effects, the order of logs, StrictMode double mounts, parent and child order and a key reset.

Function components have no lifecycle methods, yet they obviously have a lifecycle. A component appears on screen, changes many times, and eventually disappears. Knowing exactly what React does at each of those moments, and in what order, explains a large share of confusing behavior: effects that run twice in development, logs that appear in a surprising order, state that resets when it should not.

In this lesson you will follow a function component through its three phases, watch the real order of renders, effects and cleanups in the console, and learn the habit that replaces lifecycle thinking: describe what you want to stay in sync, not when to run code. If you know class components, the class lifecycle lesson has a table mapping the old methods to hooks; here we go deeper on how it works from the function's point of view.

Render, commit, effects

Every time something changes, React does the work in three stages.

  1. Render. React calls your component function to find out what the UI should look like. This is a pure calculation, and it may be repeated, paused or thrown away.
  2. Commit. React applies the smallest set of changes to the real DOM. Refs are attached now, and layout effects run.
  3. Effects. After the browser has had a chance to paint, React runs your useEffect functions. Before running an effect again, or when the component leaves, it first runs the previous run's cleanup.
flowchart LR
    T["Trigger<br/>mount, setState, new props"] --> R["Render<br/>call the function"]
    R --> C["Commit<br/>update the DOM"]
    C --> P["Browser paints"]
    P --> E["Effects<br/>cleanup of old, then setup of new"]

Every change follows the same pipeline. The component function only does the first step; effects come last.

The three phases of a component's life are these same stages seen from outside:

  • Mount: the first render, commit and effect run. Every effect runs its setup.
  • Update: any later render. Only effects whose dependencies changed run: cleanup of the old values first, then setup with the new ones.
  • Unmount: the component is removed. All cleanups run, and no setup does.

Watching the phases

The component below logs from its body, from an effect that should run only on mount, and from an effect that depends on count. Open the console, click the counter, and then hide the panel.

Mount, update and unmount in the console

The console tells the whole story:

  • Mount: render, count 0, then mount effect, then count effect: 0. Effects run in the order they are written.
  • Update (click): render, count 1, then count cleanup: 0, then count effect: 1. The old cleanup sees the old value, because it closes over the render that created it. The [] effect does not run again, because nothing in its list changed.
  • Unmount (Hide): unmount cleanup and count cleanup: 1. Only cleanups run; there is no setup.
  • Show again: a brand-new instance mounts, state starts at 0 and the sequence from the first bullet repeats.

Notice what is not in the log: nothing runs "on update" for the [] effect, and no function is called "on mount" at all. There are only renders and effects with dependency lists. The mount and unmount phases are just the cases where the list goes from "nothing" to "everything" and back.

Strict Mode and the extra mount

In development, wrapping your app in <StrictMode> (every project template does) makes React rehearse an unmount and mount right after the first mount, to prove your effects clean up properly. State and refs survive this rehearsal.

The development-only mount, unmount, mount

You should see mount effect, unmount cleanup, mount effect. Production builds run only the first line. This is why "run something exactly once when the component first appears" is the wrong goal for an effect: a correct effect can be set up, torn down and set up again any number of times. If a second run breaks your code (a duplicate subscription, a double-counted analytics event), the cleanup is incomplete. The cleanup lesson shows how to fix such cases, and You Might Not Need an Effect covers work that does not belong in an effect at all.

Parents and children

With a tree of components the order matters: React renders from the top down, but effects finish from the bottom up, because a parent's setup might depend on its children being ready.

Order of renders and effects in a tree

On load the console reads render Parent, render Child, effect Child, effect Parent. Each click on the button logs only render Parent and render Child: the child re-renders because its parent did (the usual rule; see rendering in depth), but neither [] effect runs again. Render order is parent first, effect order is child first.

Patterns for each phase

Because there are no lifecycle methods, you express each need with an effect and a dependency list.

  • Do something when the component appears. Use useEffect(setup, []) with a cleanup that reverses it, such as starting a connection and closing it.
  • React to a value changing. Put the value in the dependency array. The effect re-runs with cleanup-then-setup whenever it changes. See effect dependencies.
  • Do something when the component disappears. Return a cleanup function. It runs on unmount, and before every re-run.
  • Run on every render. Omit the array. This is rare, and usually a sign that the work belongs in the render body or an event handler.
  • Skip the first run. Sometimes you really want "only on updates". A ref can remember that the first run happened, but beware: Strict Mode keeps refs during its rehearsal, so the effect may run on that second "mount". Often the better question is whether the code should react to the change at all, or whether an event handler should do it.
  • Reset everything. To unmount and mount a fresh instance on purpose, change its key. React treats a new key as a different component, so state is discarded and effects run their full cycle. See state and keys and reconciliation.

Here is the reset in action, with a tiny component that counts its own mounts.

A new key means a fresh lifecycle

Type something, then switch users. The text disappears, and the console shows unmounted for Ana followed by mounted for Ben (mount number 2). Remove key={user} and the draft survives the switch, because React sees the same component in the same position and only updates its props; the [] effect would still believe the user is Ana, a stale-closure bug that the dependency rules exist to prevent.

Common mistakes

  • Treating [] as "componentDidMount" while reading props or state inside. If the effect uses user, it needs user in the list, or a key, not an empty array and a lint suppression.
  • Putting side effects in the component body. Logging is harmless, but starting a timer or a request in the body repeats on every render. Render is the first stage, not the effect stage.
  • Forgetting that cleanup sees old values and expecting the new ones.
  • Reading the DOM during render. The DOM does not match the new render until commit; read it in an effect, or in a layout effect if you must measure before paint.
  • Fighting Strict Mode with module-level flags to make an effect run once. Fix the cleanup instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In which order does React perform the three stages for an update?

    React calls the component to render, applies the changes to the DOM in the commit, and runs effects afterwards.
  2. 2. A component has useEffect(() => { ... return cleanup; }, [id]). When does cleanup run?

    Cleanup runs before the next setup (with the values from the render that created it) and on unmount.
  3. 3. A parent and its child both have effects with []. On the first mount, which effect runs first?

    Rendering goes from the top down, but effects complete from the bottom up, so the child's effect runs before the parent's.
  4. 4. In development with StrictMode, what does a [] effect with a cleanup do when the component first mounts?

    React rehearses an unmount and mount to verify that cleanup reverses setup. Production builds run setup once.
  5. 5. How do you force a component to unmount and mount again with fresh state?

    A changed key tells React it is a different component instance, so the old one unmounts and a new one mounts with initial state.

Summary

  • Each change goes through render (pure calculation), commit (DOM update) and effects (side effects after paint).
  • A function component's life is mount, update and unmount, but your code only has renders and effects with dependency lists; mount and unmount are the first and last runs.
  • Cleanups run before the next setup and on unmount, with the values of the render that created them.
  • Renders go parent first, effects finish child first, and Strict Mode rehearses an extra setup, cleanup, setup on mount in development.
  • Change a component's key to reset it completely, and think "keep this in sync with these values" instead of "run this on mount".

Next: Context