🌙 useReducer
/**
* useReducer: (reducer, state) => [state, dispatch]
* reducer: (state, action) => nextState
* initialState: state
* action: {type: string, ...others}
* */
function useMyReducer(reducer, initialState) {
const [state, setState] = useState(initialState)
const dispatch = action => {
setState(s => reducer(s, action))
}
return [state, dispatch]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13