我在我的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"
代码:
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例子!
感谢您的耐心等待。
您可以发布'sidebar'文件的内容吗? – Pcriulan
好吧,我添加了'sidebar'代码。但是这个文件就像'posts'一样。如果我删除'combineReducers'上的侧边栏。我也有同样的错误 – AnnatarHe