gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:51:57 25 4
gpt4 key购买 nike

我写了这段代码来创建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com