1

我似乎有一个奇怪的错误。我目前以同构方式使用 Redux,并且还包括redux-thunk作为异步操作的中间件。这是我的商店配置的样子:

// Transforms state date from Immutable to JS
const transformToJs = (state) => {
  const transformedState = {};

  for (const key in state) {
    if (state.hasOwnProperty(key)) transformedState[key] = state[key].toJS();
  }
  return transformedState;
};


// Here we create the final store,
// If we're in production, we want to leave out development middleware/tools
let finalCreateStore;
if (process.env.NODE_ENV === 'production') {
  finalCreateStore = applyMiddleware(thunkMiddleware)(createStore);
} else {
  finalCreateStore = applyMiddleware(
    createLogger({transformer: transformToJs}),
    thunkMiddleware
  )(createStore);
}

// Exports the function that creates a store
export default function configureStore(initialState) {
  const store = finalCreateStore(reducers, initialState);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('.././reducers/index', () => {
      const nextRootReducer = require('.././reducers/index');
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}

奇怪的是,我认为这个文件没有任何问题,因为我createLogger的应用很好。它会注销我所有的动作和状态,但是当我在动作创建者中返回一个函数而不是一个对象时,执行就会丢失。我尝试过debugger添加语句,这些语句从未命中和重新排序中间件似乎也无济于事。

createUser(data) {
    // This `debugger` will hit
    debugger;
    return (dispatch) => {
     // This `debugger` will NOT hit, and any code within the function will not execute
      debugger;
      setTimeout(() => {
        dispatch(
          AppActionsCreator.createFlashMessage('yellow', 'Works!')
        );
      }, 1000);
    };
  },

有没有人经历过这样的事情?

4

1 回答 1

1

哦!我没有派出行动。我只是打电话给动作创建者。必须习惯使用 Redux!

我如何认为我正在调用一个动作: AppActionCreators.createFlashMessage('some message');

如何在 Redux 中实际调用操作: this.context.dispatch(AppActionCreators.createFlashMessage('some message'));

wheredispatch是 Redux store 提供的一个方法,可以通过 React 传递给应用的每个子组件childContextTypes

于 2015-11-25T05:54:43.033 回答