1
0
Fork 0
Browse Source

vault backup: 2022-03-29 17:29:34

master
YuJian 3 years ago
parent
commit
a4adc701f5
  1. 14
      随时随地/简单的 React 思考 - useReducer.md

14
随时随地/简单的 React 思考 - useReducer.md

@ -5,7 +5,8 @@
const [state, dispatch] = useReducer(reducer, initialArg, init); const [state, dispatch] = useReducer(reducer, initialArg, init);
``` ```
React 官网的 useReducer 实现计数器案例 React 官网的 useReducer
```javascript ```javascript
const initialState = {count: 0}; const initialState = {count: 0};
@ -31,3 +32,14 @@ function Counter() {
); );
} }
``` ```
最简的 useReducer 实践
```javascript
const [state, dispatch] = useReducer((state, action) => (
{ ...state, action }),
initState
)
```
在这个最简单的事件中,useReducer 就充当了 useState 的替代,dispatch 修改状态,返回最新的 state
Loading…
Cancel
Save