Reactjs 如何从其他组件更改减速器中设置的初始状态

Reactjs 如何从其他组件更改减速器中设置的初始状态,reactjs,react-redux,Reactjs,React Redux,我已经在redux reducer中为登录验证设置了初始状态。如果登录成功,我想更改它。单击注销时,我想设置authror=' 我可以更改为组件的只读状态吗 组件 const {authError} = this.props; this.setState(authError = "") 减速器 const initState = { authError : '', empname : 'test', phoneno:'321' } const signin = (st

我已经在redux reducer中为登录验证设置了初始状态。如果登录成功,我想更改它。单击注销时,我想设置
authror='

我可以更改为组件的只读状态吗

组件

   const {authError} = this.props;
   this.setState(authError = "")
减速器

 const initState = {
 authError : '',
 empname : 'test', phoneno:'321' 
 }

 const signin = (state = initState, action) => {

switch(action.type){
  case 'SIGN_IN':
    const empname = action.payload.empname;
    const phoneno = action.cred.phoneno;
    console.log('===============$$$=====================');
    console.log(empname, phoneno);
    console.log('===============$$$=====================');
    return {
      ...state,
      authError : null,
      empname,
      phoneno
   }
} 

不,您不能在道具上调用
setState
。As道具用于在创建组件时自定义组件,并为其提供不同的参数

与道具不同,状态是一个私有特性,它严格属于单个组件。状态允许React组件根据特定事件随时间动态更改输出

reducer是一个纯函数,它将以前的状态和操作作为参数,并返回新状态

简言之,reducer以道具的形式将应用状态分配给合适的组件。如果要更改组件的prop值,则需要定义一个单独的操作并修改该操作中的值&然后可以返回修改后的值


您可以很容易地开始阅读

reducer的只读状态是什么意思?