Reactjs Redux Thunk:操作必须是普通对象
我已经浏览了这里大部分关于这类问题的帖子,但我仍然没有运气。这就是我的操作,与redux thunk npm自述文件中的示例相同:Reactjs Redux Thunk:操作必须是普通对象,reactjs,react-redux,redux-thunk,Reactjs,React Redux,Redux Thunk,我已经浏览了这里大部分关于这类问题的帖子,但我仍然没有运气。这就是我的操作,与redux thunk npm自述文件中的示例相同: const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; function increment() { return { type: INCREMENT_COUNTER }; } function incrementAsync() { return dispatch => { setTimeo
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
function increment() {
return {
type: INCREMENT_COUNTER
};
}
function incrementAsync() {
return dispatch => {
setTimeout(() => {
// Yay! Can invoke sync or async actions with `dispatch`
dispatch(increment());
}, 1000);
};
}
存储设置如下,并传递给提供商:
export default createStore(
rootReducer,
applyMiddleware(thunk)
);
<Provider store={store}>
incrementAsync从连接的组件中调度,如下所示:
this.props.dispatch(incrementAsync);
我仍然得到未捕获的错误:操作必须是普通对象。使用自定义中间件进行异步操作。
NPM版本:
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"redux": "^4.0.0",
"redux-thunk": "^2.2.0"
是否有任何设置不正确的地方?应该是
this.props.dispatch(incrementAsync())代码>
而不是this.props.dispatch(incrementAsync)接头组件处的代码>
this.props.dispatch(incrementAsync)
取代
this.props.incrementAsync();
并在连接中绑定incrementAsync函数,如
connnect(null, { incrementAsync }) (Component)
非常感谢。在这件事上把我的头发扯了好几个小时!
connnect(null, { incrementAsync }) (Component)