2017-08-15 23 views
-1

我在我的reactjs應用此錯誤:reactjs店減速方式不正確

Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.

我的店鋪看起來是這樣的:

import {applyMiddleware, combineReducers, createStore} from "redux"; 
import thunk from "redux-thunk"; 

const store = createStore(
    combineReducers({}), 
    applyMiddleware(thunk) 
); 

export default store; 

應該如何樣子是否正確?

+2

好了,你沒有在您的商店的任何減速,所以ofcourse它不是有效? – Alex

回答

1

你會得到這個錯誤,直到你添加一個減速器到combineReducers看下面的例子。

計數器減速 '/減速器/計數器'

export default (state = 0, action) => { 
    switch (action.type) { 
    case 'INCREMENT': 
    return state + 1; 
    case 'DECREMENT': 
    return state - 1; 
    default: 
    return state; 
    } 
}; 

商店

import {applyMiddleware, combineReducers, createStore} from "redux"; 
import thunk from "redux-thunk"; 
import counter from '/reducers/counter'; 

const store = createStore(
    combineReducers({counter}), 
    applyMiddleware(thunk) 
); 

export default store; 
+0

我需要什麼減速機? – Felix

+0

@Felix對於管理狀態,你應該谷歌'redux'並找出你是否真的需要使用它。看到這個例子:https://codesandbox.io/s/lOmyoxQw1 – Purgatory

1

如果你不打算使用減速機分解,那麼爲什麼要使用combineReducers?只需將以下函數定義爲您的唯一reducer並將其傳遞給createStore()。

(state = {}, action) => { 
    switch (action.type) { 
    default: 
     return state 
    } 
} 

或者乾脆如果你只是希望它的工作如下:

(state = {}, action) => state