---
title: "Cannot update a component while rendering a different component"
description: "React warns when one component sets another component's state during render. Learn why this breaks rendering rules and how to move the update into an event, an effect or derived data."
url: https://www.danny.engineering/answer/react/cannot-update-a-component-while-rendering-a-different-component
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["React"]
---

# Cannot update a component while rendering a different component

## The short answer


A child component is calling a state setter that belongs to a parent, or to a shared store, while the child is rendering. React only allows a component to update its own state during render. Move the update into an event handler or an effect, or remove the state and derive the value instead.


```diff
function Child({ items, onCount }: Props) {
-  onCount(items.length); // runs during render
+  useEffect(() => {
+    onCount(items.length);
+  }, [items.length, onCount]);
   return <List items={items} />;
 }
```


## Why it happens


Rendering is supposed to be a pure calculation: given props and state, return some JSX. While React is rendering component B, it is in the middle of working out what the screen should look like. If B changes the state of component A at that moment, A's earlier output is now out of date and React has to go back and render A again, possibly in a loop.


React handles this, but it logs the warning because the pattern is fragile and often hides a design problem. The full message names both components, for example "Cannot update a component (`App`) while rendering a different component (`Child`)", and points at the line in the stack trace.


Typical causes are:

- Calling a callback prop such as `onChange` or `onCount` directly in the body of a child.
- Dispatching to a global store (Redux, Zustand, a context setter) during render.
- Calling a parent's setter inside a `.map` or a helper function that runs while rendering.
- A form library or router hook that updates shared state during render because of how it is called.

## How to fix it


### Derive the value in the parent


Often the parent does not need the child to report anything. If the parent already has the data, compute what it needs from it directly.


```typescript
function App() {
  const items = useItems();
  const count = items.length; // no state, no callback

  return (
    <>
      <Header count={count} />
      <Child items={items} />
    </>
  );
}
```


This is the cleanest fix because there is nothing to keep in sync.


### Update in response to an event


If the parent should change when the user does something in the child, call the callback from the event handler, not from the render body.


```typescript
function SearchBox({ onSearch }: { onSearch: (query: string) => void }) {
  return <input onChange={(event) => onSearch(event.target.value)} />;
}
```


### Move it into an effect


When the update really must follow a render, such as reporting a measured value or syncing with an external store, put it in `useEffect`. The effect runs after React has committed the render, so the update starts a fresh render instead of interrupting one. Keep the dependency list accurate, and wrap callback props in `useCallback` in the parent if they would otherwise change every render.


### Lift the state up


If two components both need to read and change the same value, move the state to their closest common parent and pass it down. Neither child then has to push changes upward during render.


## Common pitfalls

- Fixing the warning with `setTimeout`. It defers the update but leaves the extra render, and makes the order of updates harder to follow.
- Moving the update into an effect without a dependency array. The effect then runs after every render and can cause a loop.
- Treating it as harmless because the screen looks right. The extra renders cost time and can produce flicker or inconsistent UI.
- Looking only at the component named first. The second name in the message is the one doing the update.

## Related reading

- [React: Keeping components pure](https://react.dev/learn/keeping-components-pure)
- [React: Sharing state between components](https://react.dev/learn/sharing-state-between-components)
