2017-05-28 55 views
0

我正在嘗試將Redux集成到reactjs.I中,在開發工具中發現了以下錯誤消息。我可以知道這究竟是什麼,我們該如何解決它?未捕獲(承諾)錯誤:操作必須是普通對象。使用自定義中間件進行異步操作

Uncaught (in promise) Error: Actions must be plain objects. Use custom middleware for async actions. 
    at dispatch (createStore.js:158) 
    at eval (middleware.js:22) 
    at eval (middleware.js:67) 
    at Object.eval [as callApi] (bindActionCreators.js:5) 
    at HomePage.componentDidMount (eval at ./app/containers/HomePage/index.js (0.chunk.js:154), <anonymous>:101:28) 
    at HomePage.proxiedComponentDidMount (eval at ./node_modules/react-proxy/modules/createPrototypeProxy.js (0.chunk.js:943), <anonymous>:61:40) 
    at eval (ReactCompositeComponent.js:265) 
    at measureLifeCyclePerf (ReactCompositeComponent.js:75) 
    at eval (ReactCompositeComponent.js:264) 
    at CallbackQueue.notifyAll (CallbackQueue.js:76) 
+0

我在你的代碼中看到一個文件文件:'''bindActionCreators.js''',所以你想使用動作創建器。你是否正確安裝了'''''''''''''''''' https://github.com/gaearon/redux-thunk#installation – dsdenes

回答

0

我想你是在dispatch函數中傳遞一個函數,默認情況下是不支持的。如果你想做這樣的事情,你應該使用thunk中間件。

import {createStore, compose, applyMiddleware} from 'redux'; 
import thunk from 'redux-thunk'; 
import rootReducer from '../reducers'; 

function configureStoreProd(initialState) { 
    const middlewares = [ 
    // Add other middleware on this line... 

    // thunk middleware can also accept an extra argument to be passed to each thunk action 
    // https://github.com/gaearon/redux-thunk#injecting-a-custom-argument 
    thunk, 
    ]; 

    return createStore(rootReducer, initialState, compose(
     applyMiddleware(...middlewares) 
    ) 
); 
} 
+0

我會在哪裏添加這些代碼,或者我需要將其添加到store.js文件中? – gitu

相關問題