<概要>
setCount(prevCount => prevCount + 1);
const [count, setCount] = useState(0);
const plusThreeDirectly = () => [0, 1, 2].forEach((_) => setCount(count + 1));
// 1しか加算されない(上書きされるイメージ)
const plusThreeWithFunction = () => [0, 1, 2].forEach((_) => setCount((c) => c + 1));
// 3加算される
「state 変数はそのコンポーネントのレンダリングごとで一定なの。よって plusThreeDirectly() はそ のレンダリング時点での count が 0 だったとしたら、それを 1 に上書きする処理を 3 回繰り返すこ とになるわけ。だから state 変数を相対的に変更する処理を行うときは、前の値を直接参照・変更 するのは避けて必ず setCount((c) => c + 1) のように関数で書くべきなのね」