2016-05-13 15 views
2

我在我的react和redux项目中遇到了错误。我没有找到如何解决它。redurs在redux中获得Function not Object,它有什么问题?

这是错误消息:

The previous state received by the reducer has unexpected type of "Function". Expected argument to be an object with the following keys: "posts", "sidebar"

error message

代码:

store.js:

import { createStore, applyMiddleware } from 'redux' 
import logger from 'redux-logger' 
import thunk from 'redux-thunk' 

import reducers from '../reducers/index' 

const middleware = process.env.NODE_ENV === 'production' ? [ thunk ] : [ thunk, logger() ] 

const store = createStore(reducers, applyMiddleware(...middleware), window.devToolsExtension ? window.devToolsExtension() : f => f) 

export default store 

减速器/索引.js文件:

import { combineReducers } from 'redux' 
import posts from './posts' 
import sidebar from './sidebar' 

const rootReducer = combineReducers({ 
    posts, 
    sidebar 
}) 

export default rootReducer 

减速/ posts.js:

import { 
    RECEIVE_POSTS, 
    RECEIVEING_POSTS 
} from '../containers/index' 

const initialState = { 
    posts: [], 
    post: {}, 
    message: 'done' 
} 

const post = (state = initialState, action) => { 
    switch (action.type) { 
     case RECEIVE_POSTS: 
      return Object.assign({}, state, { 
       posts: action.posts 
      }) 
      break 
     case RECEIVEING_POSTS: 
      return Object.assign({}, state, { 
       message: 'loading' 
      }) 
     default: 
      return state 
    } 
} 

export default post 

减速器/ sidebar.js

import { 
    TOGGLE_SIDEBAR 
} from '../containers/index' 

const initialState = { 
    show: true 
} 

const sidebar = (state = initialState, action) => { 
    switch (action.type) { 
     case TOGGLE_SIDEBAR: 
      return Object.assign({}, state, { 
       show: ! state.show 
      }) 
      break 
     default: 
      return state 
    } 
} 

export default sidebar 

我读Redux的例子,但我不知道什么是错我的代码。它只是抛出错误。

这些代码非常像redux例子!

感谢您的耐心等待。

+1

您可以发布'sidebar'文件的内容吗? – Pcriulan

+0

好吧,我添加了'sidebar'代码。但是这个文件就像'posts'一样。如果我删除'combineReducers'上的侧边栏。我也有同样的错误 – AnnatarHe

回答

1

这是曾经工作过的老:

const store = createStore(reducers, applyMiddleware(...middleware), window.devToolsExtension ? window.devToolsExtension() : f => f) 

现在,你必须把它转换为:

const store = redux.createStore(
    rootReducer, 
    initialState, 
    redux.compose(
     redux.applyMiddleware(...middleware), 
     window.devToolsExtension ? window.devToolsExtension() : f => f 
    ) 
); 
0

发生此错误的原因是您的状态对象的键必须与您传递给combineReducers的对象的键匹配。检查这个文档了解更多详情combineReducers

+0

我知道。发送给减员的“国家”是本地国家,而不是全球。我想知道我究竟在哪里错了。我真的检出了很多次redux例子,但是我不知道该怎么办: – AnnatarHe

+0

哦,我可以看到这个错误,在'reducer/posts.js'中你的reducer函数应该是'posts'而不是'post' –

0

我知道...

的PARAMS是createStore第二initialState中间件..

相关问题