Cannot update a component while rendering a different component

React JavaScript Library

Severity: Minor

What it means

React, in development: Cannot update a component (`App`) while rendering a different component (`Search`). To locate the bad setState() call inside `Search`, follow the stack trace as described in https://react.dev/link/setstate-in-render
The second name is the component doing it: while rendering, it changed the state of the first.

Affected Models

  • React 16.8 and later

How to Fix It

  1. Follow the stack trace in the console to the set call inside the second component.

  2. Often a child calls a function from its parent's props in its body, such as onChange(value) on every render. Call it from an event handler instead, or from useEffect when it must follow a change.

  3. Updating a shared store or a context's state during render? Same fix: move the update out of the render, into an event handler or an effect.