简体   繁体   English

同构Redux应用程序未注册Redux-Thunk?

[英]Isomorphic Redux app not registering Redux-Thunk?

I seem to have a weird bug. 我似乎有一个奇怪的错误。 I'm currently using Redux isomorphically and am also including redux-thunk as the middleware for async actions. 我目前同构地使用Redux,也包括redux-thunk作为异步操作的中间件。 Here's what my store config looks like: 这是我的商店配置:

// 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;
}

The weird part about this is that I don't think there's anything wrong with this file because my createLogger is applied just fine. 关于这个的怪异部分是我认为该文件没有任何问题,因为我的createLogger应用得很好。 It logs out all my actions and state, but the minute I return a function instead of an object in an action creator, the execution is lost. 它注销了我所有的动作和状态,但是当我在动作创建者中返回函数而不是对象时,执行丢失。 I've tried throwing in debugger statements, which never hit and reordering the middleware also doesn't seem to help. 我试过抛出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);
    };
  },

Has anyone experienced something like this before? 有没有人经历过这样的事情?

DOH! DOH! I wasn't dispatching the action. 我没有派遣行动。 I was only calling the action creator. 我只是打电话给动作创建者。 Gonna have to get used to that with Redux! 将不得不适应Redux!

How I thought I was invoking an action: AppActionCreators.createFlashMessage('some message'); 以为我正在调用一个动作: AppActionCreators.createFlashMessage('some message');

How to actually invoke an action in Redux: this.context.dispatch(AppActionCreators.createFlashMessage('some message')); 如何在Redux中实际调用动作: this.context.dispatch(AppActionCreators.createFlashMessage('some message'));

Where dispatch is a method provided by the Redux store, and can be passed down to every child component of the app through React's childContextTypes 其中dispatch是Redux存储提供的一种方法,可以通过React的childContextTypes向下传递到应用程序的每个子组件

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

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