gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:38:33 25 4
gpt4 key购买 nike

我似乎遇到了一个奇怪的错误。我目前正在使用 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);
};
},

有人经历过类似的事情吗?

最佳答案

卫生部!我没有 dispatch 行动。我只是给 Action 创建者打电话。我必须要习惯 Redux!

我如何认为我正在调用一个 Action :AppActionCreators.createFlashMessage('一些消息');

如何在 Redux 中实际调用操作:this.context.dispatch(AppActionCreators.createFlashMessage('一些消息'));

其中 dispatch 是 Redux 存储提供的方法,可以通过 React 的 childContextTypes 传递给应用程序的每个子组件

关于javascript - 同构 Redux 应用程序未注册 Redux-Thunk?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com