将Redux.createStore与Immutable一起使用会引发“减少器不是函数(如何将Redux与Immutable.js和composeEnhancers一起使用)

阿伦迪尔

我写了这段代码来创建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()s内部包含一个普通对象

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

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

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

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

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

未捕获的TypeError:减速器不是函数

在CombineReducers.js:39

在Array.forEach()

在CombineReducers.js:36

在Map.withMutations(immutable.js:1353)

在CombineReducers.js:35

在computeNextEntry(:2:27469)

在recomputeStates(:2:27769)

在:2:31382

分派时(createStore.js:165)

在createStore(createStore.js:240)

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

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

标记物

内置combineReducers函数希望看到的状态是纯Javascript对象。

如果要让状态树成为Immutable.js Map替代,则需要使用其他功能,例如由提供的功能redux-immutable其他Redux /不可变互操作库

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

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

因此,将您的首字母Immutable.js Map作为第二个参数传递

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章