);
}
```
### 状態を保存するために `useRef` を使用しないでください
状態の保存に `useRef` を使用するのは避けてください。
If you want to store state, you should use `useState` or `useAtomState`.
いくつかの再レンダリングを防ぐために `useRef` が必要だと感じた場合は、[再レンダリングの管理方法](#managing-re-renders)を参照してください。
## 再レンダリングの管理
React で再レンダリングを管理するのは難しいことがあります。
不必要な再レンダリングを避けるためのいくつかのルールをご紹介します。
再レンダリングの原因を理解することで、常に再レンダリングを避けることができることを念頭に置いてください。
### ルートレベルで作業する
新機能での再レンダリングをルートレベルで排除することで、簡単に避けることができるようになりました。
The `PageChangeEffect` sidecar component contains just one `useEffect` that holds all the logic to execute on a page change.
その方法で、再レンダリングを引き起こす場所が 1 つだけあることを認識できます。
### Always think twice before adding `useEffect` in your codebase
Re-renders are often caused by unnecessary `useEffect`.
You should think whether you need `useEffect`, or if you can move the logic in a event handler function.
You'll find it generally easy to move the logic in a `handleClick` or `handleChange` function.
Apollo などのライブラリで `onCompleted`、`onError` などとしても見つかることがあります。
### Use a sibling component to extract `useEffect` or data fetching logic
If you feel like you need to add a `useEffect` in your root component, you should consider extracting it in a sidecar component.
Apollo フックを使用してデータ取得ロジックにも同じことを適用できます。
```tsx
// ❌ 悪い例: データが変化していなくても再レンダーを引き起こす
// useEffect を再評価する必要があるため
export const PageComponent = () => {
const [data, setData] = useAtomState(dataState);
const [someDependency] = useAtomState(someDependencyState);
useEffect(() => {
if(someDependency !== data) {
setData(someDependency);
}
}, [someDependency]);
return