减速器不更新状态

时间:2019-05-18 14:58:45

标签: reactjs redux

我的应用程序流程很简单。请求完成后,我将调用带有有效负载的操作:

export const setVoteFlagCurrentPoll = () => dispatch => {
  dispatch({
    type: SET_VOTE_FLAG_CURRENT_POLL,
    payload: true
  })
}

然后在reducer中更改如下所示的轮询对象中的一个变量:

{
  idsurvey: 10, 
  topic: "random poll question", 
  answers: Array(4), 
  voted: false
}

减速器本身:

case SET_VOTE_FLAG_CURRENT_POLL:
      return {...state, pollData: {...state.pollData, voted: action.payload}};

我的问题是变量“ voted”没有改变其值。它仍然是“ false”。有趣的是,我可以仅以console.log({...state, pollData: {...state.pollData, voted: action.payload}});的形式记录该记录器,并且可以正常工作。为什么会这样?

1 个答案:

答案 0 :(得分:0)

好,我知道了。似乎mapStateToProps函数编写不正确。

之前(无效):

const = ({user}) => ({user}); // its returning whole user reducer

之后(现在工作):

const mapStateToProps = state => {
  return {
    user: state.user //same as above (I didnt want to change the code in other components..)
    pollData: state.user.pollData //pulling pollData directly
  }
}

希望有帮助。

相关问题