React & Next.js errors · Error 04 of 7

“Cannot update a component while rendering a different component”

By Muhammad Abdullah, full-stack, mobile and ai automation developer · Last reviewed

Cannot update a component (`X`) while rendering a different component (`Y`)

Short answer

A child component calls a parent’s state setter during its render. Move that call into an event handler or a useEffect.

Common causes

  • Calling a callback prop like onChange(value) directly in the child’s body.
  • Updating a global store (context, Zustand, Redux) during render.

How to fix it

  1. Notify the parent after render instead:

                          useEffect(() => {
      onChange(value);
    }, [value, onChange]);
                        
  2. Better still, lift the state up so the parent computes it and no sync is needed.

Let’s talk about the next step

Have an idea, a stuck app, or a workflow toimprove?

Tell me what you are building, what is blocking you, and what a successful outcome looks like.