为什么 useReducer 第一个参数设置为 Value of dispatch?
Why does useReducer first parameter get set to Value of dispatch?
下面的代码类似于 useState(即 useReducer),但我找不到任何文档来解释原因。但是,我用调试器弄明白了。
const reducer = (state,action) => {
return action;
};
const [email,setEmail] = useReducer(reducer,"");
这是因为 reducer 的写法:
const reducer = (state,action) => {
return action;
};
调度值作为减速器中的第二个 action
参数接收。新状态设置为从 reducer 返回的值,即 action
。这样 useReducer
调度函数的工作方式与 useState
状态 setter 相同。这大致就是 useState
内部发生的情况,因为它在内部使用 useReducer
。
如果返回的不是 action
,它的行为就会不同。
下面的代码类似于 useState(即 useReducer),但我找不到任何文档来解释原因。但是,我用调试器弄明白了。
const reducer = (state,action) => {
return action;
};
const [email,setEmail] = useReducer(reducer,"");
这是因为 reducer 的写法:
const reducer = (state,action) => {
return action;
};
调度值作为减速器中的第二个 action
参数接收。新状态设置为从 reducer 返回的值,即 action
。这样 useReducer
调度函数的工作方式与 useState
状态 setter 相同。这大致就是 useState
内部发生的情况,因为它在内部使用 useReducer
。
如果返回的不是 action
,它的行为就会不同。