javascript - Using Redux.createStore with Immutable 抛出“reducer is not a function”(How to use Redux with Immutable.js ans composeEnhancers)

标签 javascript redux immutable.js redux-immutable

我写了这段代码来创建 Redux store:

import {combineReducers} from 'redux-immutable'

const MyAppReducers = combineReducers({
    Node1: Node1Reducer,
    Node2: Node2Reducer,
    Node3: Node3Reducer,
    Node4: Node4Reducer
})

let store = createStore(
    MyAppReducers,
    composeEnhancers(
        applyMiddleware(
            thunkMiddleware,
            ApiMiddleware,
            loggerMiddleware
        )
    )
)

此配置的结果是一个包含在 Immutable.Map() 中的普通对象:

{
    Node1: Immutable.Map(),
    Node2: Immutable.Map(),
    Node3: Immutable.Map(),
    Node4: Immutable.Map()
}

相反,我希望所有状态树都是 Immutable.Map()

所以我尝试将一个 Immutable.Map() 对象直接传递给 combineReducers():

const MyAppReducers = combineReducers(Immutable.Map({
    Node1: Node1Reducer,
    Node2: Node2Reducer,
    Node3: Node3Reducer,
    Node4: Node4Reducer
}))

但这会在控制台中引发错误:

Uncaught TypeError: reducer is not a function

at combineReducers.js:39

at Array.forEach ()

at combineReducers.js:36

at Map.withMutations (immutable.js:1353)

at combineReducers.js:35

at computeNextEntry (:2:27469)

at recomputeStates (:2:27769)

at :2:31382

at dispatch (createStore.js:165)

at createStore (createStore.js:240)

redux-immutable 的文档声明我应该将 initialState 作为 createStore 函数的第二个参数传递,但是我的第二个参数目前是我需要配置中间件和其他东西的 composeEnhancers 函数。

如何让整个状态树成为一个Immutable.Map()对象?

最佳答案

内置的 combineReducers 函数期望它看到的状态是一个普通的 Javascript 对象。

如果您希望状态树成为Immutable.js Map,您需要使用不同的函数,例如redux-immutable 提供的函数。或 other Redux/immutable interop libs .

请注意,Redux createStore 函数可以调用为:

createStore(rootReducer, initialState, composedEnhancers);
createStore(rootReducer, composedEnhancers);

因此,将您的初始 Immutable.js Map 作为第二个参数传递。

关于javascript - Using Redux.createStore with Immutable 抛出“reducer is not a function”(How to use Redux with Immutable.js ans composeEnhancers),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48083663/

相关文章:

javascript - 如何使用 angularJs 在页面中显示数组数据

javascript - TypeScript async wait 似乎并不总是等待 ("block and come back")

javascript - 对于具有自己的 props 的连接组件的 TypeScript 警告

javascript - 通过与另一个对象部分匹配来查找对象

redux - 开 Jest 测试: "state.get is not a function"

javascript - 检测组合按键(Control、Alt、Shift)?

javascript - 来自客户端的 Meteor.userId - 更改显示用户电子邮件,行为正确吗?

angular - Redux 中的状态是什么?

typescript - Redux typescript-eslint/no-unsafe-赋值 - useDispatch

immutability - 具有不可变数据结构的 RxJS?