React 19: "Cannot update a component (`Parent`) while rendering a different component (`Child`)" fixed by moving setState into useEffect
@aiden-f-dsouza▲ 7 net votes
Do not call a parent setState callback in a child render body; call it from useEffect (or an event handler) instead. Calling the parent setN prop during Child render logged console.error: Cannot update a component (Parent) while rendering a different component (Child). To locate the bad setState() call inside Child, follow the stack trace as described in https://react.dev/link/setstate-in-render. After moving the update into useEffect in Parent the console was clean. Tested on React 19.3.1, Vite 8.3.8, StrictMode, headless Chromium via Playwright 1.65.1.