繁体   English   中英

调度动作后未调用Reducer

[英]Reducer not called after action is dispatched

我正在尝试使用来自http://reactboilerplate.com的样板代码开始对react / redux进行操作,到目前为止它一直很好。

我想从容器更新我的状态,并且正在调度这样的调用:

// This is the method that should tigger the action
updateMoneyValue(newValue);

...

// Here I hope I set up everything correctly...
function mapDispatchToProps(dispatch) {
  return {
    updateMoneyValue: newMoneyValue =>
      dispatch(updateMoneyValue(newMoneyValue)),
  };
}
const withConnect = connect(
  mapStateToProps,
  mapDispatchToProps,
);

const withReducer = injectReducer({ key: 'counterPage', reducer });
const withSaga = injectSaga({ key: 'counterPage', saga });

export default compose(
  withReducer,
  withSaga,
  withConnect,
)(CounterPage);

我认为dispatch(updateMoneyValue(newMoneyValue))应该实际上是分派动作并使其到达减速器。 但是实际上发生的是,当我在第一行中执行呼叫时,触发了我的操作:

export function updateMoneyValue(moneyValue) {
  console.log('Action has fired.');
  return {
    type: UPDATE_MONEY_VALUE,
    moneyValue,
  };
}

但是减速器永远不会运行:

function counterPageReducer(state = initialState, action) {
  switch (action.type) {
    case UPDATE_MONEY_VALUE:
      console.log('Reducer was called.');
      state.set('moneyValue', action.moneyValue);
      return state;
    default:
      return state;
  }
}

我试图找到解决方案,但我真的坚持这一解决方案,不胜感激!

最好的问候,A香。

更改:

state.set('moneyValue', action.moneyValue);

至:

state = state.set('moneyValue', action.moneyValue);

编辑:这样,您可以“设置新状态”,而无需state =您正在更改状态,但不使用更改后的状态来稍后返回它

当您映射状态或调度时,您的方法将在道具中可用。

// This is the method that should tigger the action
this.props.updateMoneyValue(newValue);

当您调用updateMoneyValue(newValue) ,您没有在分派操作,而是直接在调用操作创建者。

要通过Redux,您需要调用this.props.updateMoneyValue(newValue)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM