useReducer
根拠を探そうとreactのコード読んでみようとしたが、import/exportのたらい回しが複雑すぎて断念した code:ts
const reducer = <State>(prev: State, value: State | ((prev: State) => State)) =>
typeof action === "function" ? value(prev) : value;
export const useState = <State>(initialState: State | (() => State)): [
State,
(value: State | ((prev: State) => State)) => void
] =>
typeof initialState === "function" ?
useReducer(reducer, null, initialState) :
useReducer(reducer, initialState);
Array.prototype.reduceにわたす函数のことをreducerと呼ぶ 実際、ReactのreducerもArray.prototype.reduceに渡せる
シンプルでうまい使い方
わかりやすそうな例
code:ts
const reducer = (value) = (state, action) => { /*...*/ };
// ...