2017-11-28 4 views
1

おはようございます、createStore関数で問題が発生しているようです。combine-Redux createStoreは、combineReducerが渡されたときに「エラー:減速機が関数になることを期待しました」を返します

これは全体store.jsファイル

// Import required redux functions 
import { createStore, applyMiddleware, compose } from 'redux'; 
// Import thunk for async redux reducers 
import thunk from 'redux-thunk'; 
// import the main reducer 
import rootReducer from './reducers'; 
console.log(rootReducer); // log the value of the root reducer to the console for inspection 
// Define the store as a constant so it acts like a singleton (for authentication reasons this is helpfull) 
const store = createStore(rootReducer, compose(
    applyMiddleware(thunk), 
    window.devToolsExtension ? window.devToolsExtension : f => f 
)); 
console.log(store); // log the store to the console for inspection (does not happen) 
// export it as the default object 
export default store; 

とシンプルなreducers/index.jsファイルです。

// Import the core function 
import { combineReducers } from 'redux'; 
// Import all the reducers 
import loginReducer from './loginReducer'; // a simple working reducer for handling the jwt token 

console.log(loginReducer); // log the reducer for inspection 
// export the combination of reducers within their own subroutes 
const rootReducer = combineReducers({ 
    // TODO add reducers with keys like so 
    login: loginReducer, 
}); 

export default rootReducer; 

そしてloginreducer.jsファイル:

// the default state configuration 
const initialState = {logged_in: false, error: null, token: null} 
// login reducer 
export default (state = initialState, action) => { 
    switch (action.type) { // check the type of action that was passed in 
    case "LOGIN_ERROR": // error during the login process 
     return {logged_in: false, error: action.error, token: null} 
    case "LOGIN_SUCCESS": // login was successfull or.... 
    case "LOGIN_NEW_TOKEN": // client has recived a new token 
     return {logged_in: true, error: null, token: action.token} 
    case "LOG_OUT": // the client has logged out 
     localStorage.removeItem('token'); // remove the token from storage 
     return initialState // return the inital state (not logged in) 
    default: 
     return state // return the current state (a.k.a take no action) 
    } 
} 

これは、カスタムエラーをスローError: Expected the reducer to be a functionに定義されました46行目(の、構築されたバージョンでは55):しかし

if (typeof reducer !== 'function') { 
    throw new Error('Expected the reducer to be a function.'); 
    } 

、私はtemp1としてコンソール()にrootReducerを保存し、それは確かに"function"で、型のチェックするためにクロムデバッグツールを使用しています。

chrome_debugging_screenshot

createStore.jsで条件にブレークポイントを追加するとreducerundefinedに設定されていることを私に示して、それはconsole.logコールのために定義することができますが、それは上の関数に渡されたときに定義されていませんどのように任意のアイデア次の行?

私が間違っていることに関するアイデアはありますか?

+0

loginReducerのコードも貼り付けることができますか? – klugjo

+0

@klugjo確かに、私は今それを追加しました。それはかなり一般的な助けを確認していない –

+0

私はそれを得た。私はそれを完全に逃したとは信じられません... –

答えて

1

おはよう、

問題が見つかりました。 in store.jsdevToolsExtensionの呼び出しの後に()を追加するのを忘れました。言い換えれば、この:

window.devToolsExtension ? window.devToolsExtension() : f => f 

シンプルだが迷惑:

window.devToolsExtension ? window.devToolsExtension : f => f 

は次のようになります。

関連する問題