已触发操作,但状态未在reducer中更新

时间:2016-08-09 13:37:39

标签: javascript reactjs redux

我有一个基本的动作,可以解雇许多事件。

export const ADDED = 'cartridge/ADDED';
export const REMOVED = 'cartridge/REMOVED';
export const SUBSCRIBE = 'cartridge/SUBSCRIBE';
export const UNSUBSCRIBE = 'cartridge/UNSUBSCRIBE';
export const SUBSCRIPTION_READY = 'cartridge/SUBSCRIPTION_READY';
export const SUBSCRIPTION_ERROR = 'cartridge/SUBSCRIPTION_ERROR';

export function subscribe() {
  return (dispatch, getState) => {
    const { meteor: { client: Meteor } } = getState();
    const sub = Meteor.subscribe('cartridges');
    Meteor.ddp.on('added', function (document) {
      const { name, id: _id, fields } = document;
      dispatch({ type: ADDED, payload: { _id, ...fields } });
    });

    dispatch({ type: SUBSCRIBE, payload: sub });

    sub.on('ready', () => dispatch({
      type: SUBSCRIPTION_READY
    })).on('error', payload => dispatch({
      type: SUBSCRIPTION_ERROR,
      payload
    }));
  }
}

最后,在我的减速机上,我做了以下(删节)。

export default function cartridges(state={
  data: {},
  isReady: false,
  isSubscribed: false
}, action) {
  switch (action.type) {
    // ...
    case SUBSCRIPTION_READY:
      return Object.assign({}, state, {
        isReady: true
      });
    default:
      return state;
  }
}

然而,当这个动作发生时(可以在redux-logger中看到),它永远不会捕获减速器中的动作。为什么会这样?

0 个答案:

没有答案