Reducers may not dispatch actions

来源:互联网 发布:java语言程序设计答案 编辑:程序博客网 时间:2024/05/20 23:38

今天在使用redux的时候出现一个bug

Reducers may not dispatch actions

情景如下

                  onOk={() => {                    this.props.changeSubmitModalState();                    this.props.postOrderCombine(this.props.carOrders, ((time) => {                      const hide = message.loading('提交订单中, 请等待...', 0);                      return () => {                        hide();                        message.success('提交订单成功!', time);                        console.log('clear');                        this.props.emptyOrder();                      };                    })(2));

查查文档


A Note for Flux Users

If you attempt to call dispatch from inside the reducer, it will throw with an error saying “Reducers may not dispatch actions.” This is similar to “Cannot dispatch in a middle of dispatch” error in Flux, but doesn't cause the problems associated with it. In Flux, a dispatch is forbidden while Stores are handling the action and emitting updates. This is unfortunate because it makes it impossible to dispatch actions from component lifecycle hooks or other benign places.

In Redux, subscriptions are called after the root reducer has returned the new state, so you may dispatch in the subscription listeners. You are only disallowed to dispatch inside the reducers because they must have no side effects. If you want to cause a side effect in response to an action, the right place to do this is in the potentially async action creator.


所以在reducer中是不允许disp一个action的(使得redux保持pure)

1 0