javascript - Redux 存储为 immutable.js 映射

标签 javascript reactjs react-native redux immutable.js

我在构建我的 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/

相关文章:

reactjs - react /Redux : Where to process data from API response and dispatching action from reducers

javascript - 构建后,我们得到 argon-dashboard-react/static/js/main.e8eb0f76.chunk.js net::ERR_ABORTED 404 (Not Found)

android - 无法运行 react-native 应用程序

javascript - 更改 React-native 中的设备区域设置

javascript - 如何获取字符串中重复的字母?

javascript - Angular - 如何将多个 FormGroups 插入 FormArray?

javascript - 服务 png 图像的 Axios 图像损坏

javascript - svelte:svelte 如何在 if block 之间运行函数?

javascript - 错误边界回退 ui 未在 react 中渲染?

react-native - wix react 原生导航 v2 |如何从侧边抽屉组件将新屏幕推送到当前屏幕