为什么这个thunk不更新商店?

时间:2017-07-13 23:46:53

标签: redux redux-thunk

我创建了一个thunk,它发送了许多不同的动作:

export function resetEverything() {
  return function (dispatch) {
    return Promise.all([
      dispatch(updateCurrentColor('blue')),
      dispatch(updateCurrentTypeChoice('hot')),
      dispatch(updateData('fish', {})),
      dispatch(updateData('giraffes', {})),
      dispatch(updateData('elephants', {})),
      dispatch(updateData('zebras', {})),
    ]).then(() => console.log('resetEverything called'));
  };
}

这些操作也单独用于应用程序中。单独调用,它们工作正常;商店使用有效载荷进行更新。

但是,在这个批处理操作中,调度所有操作,控制台显示“resetEverything called”,甚至当我浏览Chrome中的Redux扩展时,每个操作都以相同的结构调度(具有不同的有效载荷,自然)。但是......当我看到Diff它说(states are equal)时,果然,检查状态&g​​t;树显示商店密钥根本没有更新。

为什么这不起作用?为什么要忽略调度的操作?

减速机:

import update from 'immutability-helper';

function reducer(state = initialState, action = {}) {
  switch (action.type) {
    case UPDATE_CURRENT_COLOR:
      return update(state, { currentColor: { $set: action.payload } });
    case UPDATE_CURRENT_TYPE_CHOICE:
      return update(state, { currentTypeChoice: { $set: action.payload } });
    case UPDATE_DATA:
      return update(state, { data: { [action.payload.property]: { $merge: action.payload.dataObject } } });
    default: return state;
  }
}

0 个答案:

没有答案