React「Cannot update a component while rendering a different component」の原因と直し方
Cannot update a component while rendering 警告は、set 関数を render の外で呼べば消える。
別コンポーネントの state はイベントハンドラか useEffect で更新する。
公開: 更新:
node bad.js Warning: 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://reactjs.org/link/setstate-in-render at Child (/tmp/tmp.TtBZkoe7v5/bad.js:5:18) at div at Parent (/tmp/tmp.TtBZkoe7v5/bad.js:11:39) レンダー自体は完了する(警告だけが出る)
2026-11-03 に node:20(React 18.3.1 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細
要約
Cannot update a component (A) while rendering a different component (B) は、コンポーネント B のレンダリング中に、別のコンポーネント A の状態更新関数を呼んでしまったときに出る警告である。
React ではレンダーは純粋であるべきで、描画の途中で他コンポーネントの state を書き換えてはいけない。
更新はイベントハンドラか useEffect へ移す。
実行例
node:20 で試すと、レンダー中に親の状態を更新しても描画自体は最後まで通り、警告だけが出た。
更新を useEffect に移した版では同じ結果が警告なしで得られている。
$ node bad.js
Warning: 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://reactjs.org/link/setstate-in-render
at Child (/tmp/tmp.TtBZkoe7v5/bad.js:5:18)
at div
at Parent (/tmp/tmp.TtBZkoe7v5/bad.js:11:39)
レンダー自体は完了する(警告だけが出る)
$ echo $?
0$ node solve_effect.js
警告なしで描画: {"type":"div","props":{},"children":[{"type":"span","props":{},"children":["child:7"]},{"type":"p","props":{},"children":["ready:7"]}]}
$ echo $?
0— 2026-11-03 時点の出力
検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-11-03 検証(ほか 2 件)
- 検証日
- 実行環境
node:20Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 20.20.2
- npm 10.8.2
- Python 3.11.2
- Git 2.39.5
- React 18.3.1
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. 即時実行を関数に変える
// NG: レンダー時に呼ばれてしまう
<button onClick={handleClick()}>送信</button>
// OK: クリック時に呼ばれる
<button onClick={() => handleClick()}>送信</button>イベントには「呼び出した結果」を渡さず、「関数そのもの」を渡す。
2. 副作用は useEffect に入れる
useEffect(() => {
onReady(id);
}, [id, onReady]);親へ通知するような更新は、レンダー後に一度だけ走る useEffect に移す。
3. 更新の責務を親側に置く
子のレンダー中に親 state を変えたくなったら、設計を見直す。
多くの場合、そのロジックは親のイベントハンドラや effect に置くのが正しい。
よくある原因
- ハンドラを即時実行している。
onClick={handleClick()}と書くと、クリック時ではなくレンダー時に関数が実行され、その中の setter が走る。 - レンダー中に setter を呼び出している。
子の描画中に、props で受け取った親の setter をそのまま呼んでいる。 - 外部ストアを直接更新している。
レンダー本体で store の更新関数を呼んでいる。