Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么useReducer是这样设置的?感觉就像';他在倒着写代码吗? constinitialstate={count:0}; 功能减速机(状态、动作){ 开关(动作类型){ 案例“增量”: 返回{count:state.count+1}; “减量”一案: 返回{count:state.count-1}; 违约: 抛出新错误(); } } 函数计数器(){ const[state,dispatch]=useReducer(reducer,initialState); 返回( 计数:{state.Count} 分派({type:'decrement'})}>- 分派({type:'increment'})}>+ );_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 为什么useReducer是这样设置的?感觉就像';他在倒着写代码吗? constinitialstate={count:0}; 功能减速机(状态、动作){ 开关(动作类型){ 案例“增量”: 返回{count:state.count+1}; “减量”一案: 返回{count:state.count-1}; 违约: 抛出新错误(); } } 函数计数器(){ const[state,dispatch]=useReducer(reducer,initialState); 返回( 计数:{state.Count} 分派({type:'decrement'})}>- 分派({type:'increment'})}>+ );

Javascript 为什么useReducer是这样设置的?感觉就像';他在倒着写代码吗? constinitialstate={count:0}; 功能减速机(状态、动作){ 开关(动作类型){ 案例“增量”: 返回{count:state.count+1}; “减量”一案: 返回{count:state.count-1}; 违约: 抛出新错误(); } } 函数计数器(){ const[state,dispatch]=useReducer(reducer,initialState); 返回( 计数:{state.Count} 分派({type:'decrement'})}>- 分派({type:'increment'})}>+ );,javascript,reactjs,redux,Javascript,Reactjs,Redux,这直接取自React文档。我想具体说明这一行代码: const[state,dispatch]=useReducer(reducer,initialState) 在新创建的变量state和dispatch中,我们将reducer分配给dispatch,将initialState分配给state。这在我看来是落后的。如果useReducer从函数的第二个参数(initialState)赋值,我们为什么要首先声明状态。我希望我清楚这个问题。。。我知道理解我的要求可能很难。基本上,为什么代码设置不是这

这直接取自React文档。我想具体说明这一行代码:

const[state,dispatch]=useReducer(reducer,initialState)

在新创建的变量state和dispatch中,我们将reducer分配给dispatch,将
initialState
分配给state。这在我看来是落后的。如果
useReducer
从函数的第二个参数(
initialState
)赋值,我们为什么要首先声明状态。我希望我清楚这个问题。。。我知道理解我的要求可能很难。基本上,为什么代码设置不是这样的呢

const[dispatch,state]=useReducer(reducer,initialState)

或:


const[state,dispatch]=useReducer(initialState,reducer)
useReducer
返回一个数组,该数组的第一个值是state,第二个值是dispatch。您正在分解这些值

constsomearray=()=>['value1','value2']
常量[value1,value2]=someArray()
console.log(值1)

console.log(value2)
useReducer
返回一个数组,其第一个值是state,第二个值是dispatch。您正在分解这些值

constsomearray=()=>['value1','value2']
常量[value1,value2]=someArray()
console.log(值1)

console.log(value2)
我认为选择约定是因为reacts新增了
useState()
功能。第一个参数是返回的状态,第二个参数是setter(在本例中是reducer)。我认为这是为了保持相似的编码风格。因为
state
是运行reducer的结果。这是您将使用的结果值。我认为选择该约定是因为reacts new
useState()
功能。第一个参数是返回的状态,第二个参数是setter(在本例中是reducer)。我认为这是为了保持相似的编码风格。因为
state
是运行reducer的结果。这是您将使用的结果值。谢谢您的回复。我是一个比较新手的程序员,尤其是在redux中。我的后续问题是:回调函数不也是在函数参数中第二个(或更高)列出的吗?我还没有看到过很多将函数作为第一个参数的例子,但这可能是由于经验水平。我更新了我的答案,并进行了更深入的解释,但确实有些时候,您希望回调函数成为第一个参数(例如,当参数的数量变化时),谢谢您的帮助!谢谢你的回复。我是一个比较新手的程序员,尤其是在redux中。我的后续问题是:回调函数不也是在函数参数中第二个(或更高)列出的吗?我还没有看到过很多将函数作为第一个参数的例子,但这可能是由于经验水平。我更新了我的答案,并进行了更深入的解释,但确实有些时候,您希望回调函数成为第一个参数(例如,当参数的数量变化时),谢谢您的帮助!