Cannot update a component while rendering a different component: how to fix it

IntermediateReact interview questionBy , updated 3 December 2026

Short answer

"Cannot update a component while rendering a different component" is a React development warning. It means that while component B was rendering, it called a state setter that belongs to component A: often a parent's callback, a context setter, a store action or a router navigation. Rendering must not change other components, so move that call into the event handler that caused it, into an effect, or replace the stored state with a calculated value.

What the warning means

The full text names both components:

Cannot update a component (`App`) while rendering a different component (`Filters`).
To locate the bad setState() call inside `Filters`, follow the stack trace as described in https://react.dev/link/setstate-in-render

Read it as "Filters changed App's state during its render". The component to fix is the second one, Filters.

A component may update its own state during render, and the React docs describe that pattern. Updating a different component during render is not allowed. React added this warning in version 16.13. Its release notes explain that a component should not cause side effects in other components during rendering. A render can be thrown away, restarted or run twice, so a state change made during render may happen at the wrong time or more than once.

The warning is logged with console.error in development builds only. React prints it once per rendering component, so a single line in the console can stand for many bad updates.

Cause 1: calling a parent callback during render

function Filters({ items, onCountChange }) {
  const visible = items.filter((i) => i.inStock);
  onCountChange(visible.length); // sets App's state during render
  return <List items={visible} />;
}

If the parent needs the count, calculate it in the parent, which already has items:

function App() {
  const [items, setItems] = useState([]);
  const inStockCount = items.filter((i) => i.inStock).length;
  return (
    <>
      <p>{inStockCount} in stock</p>
      <Filters items={items} />
    </>
  );
}

When the value comes from something the child does, such as a click, call the callback from that event handler. The React docs recommend updating both components in the same event, so React batches them into one render. This is lifting state up: one owner for the state, and children that report events rather than copy values.

Cause 2: setting context or store state during render

function SettingsPage() {
  const { setTitle } = useContext(LayoutContext);
  setTitle("Settings"); // updates the Layout component during render
  return <SettingsForm />;
}

Syncing a value into another component after it renders is a side effect, so it belongs in an effect:

function SettingsPage() {
  const { setTitle } = useContext(LayoutContext);
  useEffect(() => {
    setTitle("Settings");
  }, [setTitle]);
  return <SettingsForm />;
}

The same applies to Redux dispatch, Zustand actions and form library setters called in a component body.

Cause 3: navigating during render

Router navigation updates the router component's state. Calling router.push() or navigate() in a component body can trigger this warning, which is why some reports name Router or BrowserRouter as the updated component.

  • Next.js App Router: to redirect while rendering, call redirect() from next/navigation. Its docs say it can be called in Client Components during rendering, but not in event handlers. In event handlers use useRouter().push().
  • React Router: its docs describe navigate() as something to call in response to user interactions or effects. Move it into a useEffect, or render <Navigate to="/login" />.
function Protected({ user, children }) {
  const navigate = useNavigate();
  useEffect(() => {
    if (!user) navigate("/login");
  }, [user, navigate]);
  return user ? children : null;
}

How to find the bad call

  1. Read the names in the warning. The second name is the component that is rendering. The first is the one whose state changed.
  2. Expand the stack trace in the console. It leads to the line inside the rendering component that called the setter, possibly through a custom hook or a library.
  3. Look for calls in the component body that are not inside a handler, an effect or a callback: onSomething(...), dispatch(...), set...(...), navigate(...).

Which fix to choose

Situation Fix
The value can be calculated from props or state Calculate it during render, store nothing
A user action caused the change Call the setter in that event handler
You must sync with another component after render useEffect
Two components keep copies of the same value Lift the state to their common parent

An effect is the fallback, not the first choice. It costs an extra render, and it is easy to turn into a loop. Build linked inputs without effect loops practises the alternatives.

Common follow-up questions

Why can a component update its own state during render but not another's?

When a component updates itself during render, React can throw away that one render and retry it right away. The React docs say that to avoid slow cascading retries, React only allows this for the component that is rendering.

Does this warning break production?

It does not print in production, but the bug is still there. The update can apply at an unexpected time, run twice, or cause an extra render.

Is it the same as "Too many re-renders"?

No. Too many re-renders is a thrown error for a component that updates its own state on every render.

Why do I see this after upgrading a library?

The library probably started calling a setter during render. Check the stack trace. If the call comes from library code, look for a newer version or a documented way to pass the value without a render-time update.

Mistakes to avoid

  • Calling onChange, dispatch or navigate in the component body

  • Copying a child's derived value up into parent state instead of calculating it in the parent

  • Reaching for useEffect for every case, even when an event handler is the right place

  • Fixing the first component named in the warning instead of the second

  • Ignoring the warning because it does not appear in production

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Build linked inputs without effect loops: a Celsius/Fahrenheit converter and a recipe scalerMedium35 min
  2. Build a component that works both controlled and uncontrolledMedium30 min
  3. Remove prop drilling without context: pass elements through children and slot propsMedium30 min
  4. Debug an infinite useEffect() loop and prevent stale asynchronous responsesMedium35 min
  5. Predict the order of useEffect, useLayoutEffect and cleanups in parent and child componentsMedium30 min