← Back to React, page 6

react-0113 · Easy fresher

In what order do render, effects and cleanups run for a parent and a child component, including when dependencies change and when components unmount?

Rendering goes parent first, then child. After the commit, effects run child first, then parent, because React finishes children before parents. When dependencies change, the old effect's cleanup runs before the new effect. On unmount, cleanups run for the removed components. Know the sequence by running a logging example, and remember cleanup always sees the values from the render that created the effect.

Full answer

Order puzzles test whether you understand the render and commit phases.

Render phase. React calls components top-down: the parent function first, then the child. This phase should be pure.

Commit phase. React updates the DOM, then runs layout effects, then (after paint, usually) passive effects (useEffect). Effects run in child-to-parent order, like the completion of a tree walk, so the child's effect runs before the parent's effect, even though the parent rendered first.

Updates. When a dependency changes, React renders again and, after commit, first runs the cleanup of the previous effect (with the old values it captured) and then the new effect. Effects whose dependencies did not change are skipped. All cleanups of the changed effects run before any of the new effects of that commit.

Unmount. When a component is removed, its cleanup runs once. React runs the cleanups for the removed subtree, and you should not depend on a particular order between parent and child cleanups. Cleanup should only release what its own effect created.

Strict Mode in development adds an extra setup, cleanup, setup cycle on mount to expose missing cleanup. The example below runs without Strict Mode so the order is clear.

Practical consequences: a parent effect can rely on child effects having run (and child refs being attached), but should not assume order for unmount cleanups. Always write cleanup to undo exactly what the effect did.

The example logs every step for a parent and a child. Press Update to change a dependency, then Remove to unmount.

jsx
import { useEffect, useState } from 'react';

function Child({ value }) {
  console.log('  render Child ' + value);
  useEffect(() => {
    console.log('  effect Child ' + value);
    return () => console.log('  cleanup Child ' + value);
  }, [value]);
  return <p>child {value}</p>;
}

function Parent({ value }) {
  console.log('render Parent ' + value);
  useEffect(() => {
    console.log('effect Parent ' + value);
    return () => console.log('cleanup Parent ' + value);
  }, [value]);
  return <Child value={value} />;
}

export default function App() {
  const [value, setValue] = useState(1);
  const [visible, setVisible] = useState(true);

  return (
    <div>
      <button onClick={() => setValue(value + 1)}>Update</button>
      <button onClick={() => setVisible(false)}>Remove</button>
      {visible && <Parent value={value} />}
    </div>
  );
}

The mount lines come first, then the lines after Update, then the lines after Remove. Output of the run (no Strict Mode): render Parent 1 render Child 1 effect Child 1 effect Parent 1 --- press Update render Parent 2 render Child 2 cleanup Child 1 cleanup Parent 1 effect Child 2 effect Parent 2 --- press Remove cleanup Parent 2 cleanup Child 2

Key points

  • Render runs parent then child. Effects run child then parent after commit.
  • On a dependency change, the old cleanup runs before the new effect, using the old values.
  • On unmount, cleanups run once. Do not rely on cleanup order between parent and child.
  • Layout effects run before passive effects, after DOM updates.
  • Strict Mode adds an extra setup and cleanup cycle on mount in development.

Common mistakes

  • Assuming the parent effect runs before the child effect.
  • Expecting the new effect to run before the old cleanup.
  • Reading new values inside a cleanup function.
  • Relying on cleanup order between parent and child on unmount.
  • Missing the extra setup and cleanup cycle that Strict Mode adds in development.

Follow-ups

  • How do useLayoutEffect callbacks fit into this order?
  • What changes when the dependency array is empty?
  • Why does the cleanup see old values?

Tips

  • Separate render order (parent first) from effect order (child first).
  • Say the cleanup uses the values from its own render.
  • Mention Strict Mode so the interviewer sees you know the dev behaviour.