1
0
Fork 0
Browse Source

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

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

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

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