From a4adc701f525de011515a5eab733d250289623d2 Mon Sep 17 00:00:00 2001 From: YuJian Date: Tue, 29 Mar 2022 17:29:34 +0800 Subject: [PATCH] vault backup: 2022-03-29 17:29:34 --- .../简单的 React 思考 - useReducer.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/随时随地/简单的 React 思考 - useReducer.md b/随时随地/简单的 React 思考 - useReducer.md index 5a2a2e3..0bc023e 100644 --- a/随时随地/简单的 React 思考 - useReducer.md +++ b/随时随地/简单的 React 思考 - useReducer.md @@ -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() { ); } -``` \ No newline at end of file +``` + +最简的 useReducer 实践 + +```javascript +const [state, dispatch] = useReducer((state, action) => ( + { ...state, action }), + initState +) +``` + +在这个最简单的事件中,useReducer 就充当了 useState 的替代,dispatch 修改状态,返回最新的 state \ No newline at end of file