【问题标题】:Error: Expected the reducer to be a function - MERN app错误:预计减速器是一个函数 - MERN 应用程序
【发布时间】:2020-06-10 13:27:07
【问题描述】:

我正在开发一个 MERN 应用程序,代码部署在 Heroku 上并且运行良好。但由于我在我的 store.js 中为生产构建添加了三元运算符,它在开发中停止工作。

store.js

import { createStore, applyMiddleware, compose } from "redux"
import thunk from "redux-thunk"
import rootReducer from "./reducers/Layout"

const initialState = {}

const middleware = [thunk]

const store = createStore(
    rootReducer, 
    initialState, 
    compose(
        applyMiddleware(...middleware),
        (
            (process.env.NODE_ENV === 'production') ? 
             compose : 
            (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose)
        )
    )
)

export default store

我从我的 Layout.js 导入我的 rootReducer(index.js 替换),但它工作正常,直到我添加了三元运算符。但以防万一这是我的 Layout.js 中的 combineReducers 函数。

Layout.js(index.js 替换)

import { combineReducers } from "redux"
import RuleReducer from "./RuleReducer"
import ErrorReducer from "./ErrorReducer"
import AuthReducer from "./AuthReducer"

export default combineReducers({
    rule: RuleReducer,
    error: ErrorReducer,
    auth: AuthReducer
})

【问题讨论】:

  • “但是由于我添加了三元运算符......” - 那么你已经知道你必须在哪里寻找问题的根源。

标签: javascript node.js redux environment-variables mern


【解决方案1】:

好吧,所以我要么是瞎子要么是个傻瓜,但感谢 Andreas 对我的问题发表了评论,我刚刚修改了我的三元运算符,以便任何遇到同样问题的人在这里修改:

import { createStore, applyMiddleware, compose } from "redux"
import thunk from "redux-thunk"
import rootReducer from "./reducers/Layout"

const initialState = {}

const middleware = [thunk]

const stage = (process.env.NODE_ENV === 'production') ? compose : (window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())

const store = createStore(
    rootReducer, 
    initialState, 
    compose(
        applyMiddleware(...middleware),
        stage
    )
)

export default store

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2017-09-07
    • 2022-01-04
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多