我在构建我的 React native 应用程序时遇到此错误:
The preloadedState argument passed to createStore has unexpected type of "Object". Expected argument to be an object with the following keys: "height", "width"
我认为 redux store 应该是 immutable.js Map .
这是我的 createStore
代码:
import { Map } from 'immutable'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import { Volcalc } from './components/volcalc/Volcalc'
import { width } from './components/width/width.reducer'
import { height } from './components/height/height.reducer'
import React from 'react'
const initialWidth = Map({ 'width1': 20, 'width2': 0, 'width3': 0 })
const initialHeight = Map({ 'height1': 6, 'height2': 0 })
const initialState = Map({height: initialHeight, width: initialWidth})
const reducer = combineReducers({ height, width })
const store = createStore(reducer, initialState)
如果我将 initialState
更改为此,它会起作用:
const initialState = {height: initialHeight, width: initialWidth}
但现在商店不是 immuatble.js map
。如何使 redux 存储成为 immutable.js map
? (我的 reducer 确实使用 immutable.js 映射)。
最佳答案
当您使用默认 redux
包中的 combineReducers
方法时,createStore
的第二个参数始终应该是一个普通的 JavsScript 对象。
由于您需要在 createStore
中为初始状态使用不可变对象(immutable对象),因此您必须使用 redux-immutable 附带的
包。combineReducers
方法的特殊实现
您可以阅读 documentation 'redux-immutable' 包以获得更好的理解。
关于javascript - Redux 存储为 immutable.js 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42916286/