本文介绍了reducers 在 redux 中得到 Function not Object,有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 react 和 redux 项目出现错误.我没有找到如何解决它.

I got a error at my react and redux project. and I not found how to resolve it.

这是错误信息:

reducer 接收到的前一个 state 有意外的Function"类型.预期参数是具有以下键的对象: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

reducers/index.js:

reducers/index.js:

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

const rootReducer = combineReducers({
    posts,
    sidebar
})

export default rootReducer

reducers/posts.js:

reducers/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

reducers/sidebar.js

reducers/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 示例,但我不知道我的代码有什么问题.它只是抛出错误.

I read redux example but I don't know what's wrong with my code. It just throw Error.

那些代码很像 redux 的例子!!!

Those code is very like redux example!!!

感谢您的耐心等待.

推荐答案

这是过去的工作:

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

现在您必须将其转换为:

Now you have to convert it to:

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

这篇关于reducers 在 redux 中得到 Function not Object,有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 10:23