小编Dus*_*ips的帖子

redux-toolkit createSlice 可以使用 js Map 作为状态吗?

一般来说,强烈建议不要Map使用可变对象,例如.

\n

然而,沉浸式的魔力允许对不可变的对象进行操作,就好像它们是可变的一样。

\n

具体来说,immer 使用enableMapSet支持Map 的不可变版本

\n

在 redux-toolkit 中createReducercreateSlice使用 immer\'s 包装状态操作produce

\n

总的来说,我认为这些事实意味着这样的代码应该是安全的:

\n
import { createSlice } from \'@reduxjs/toolkit\'\n\nexport const testmapSlice = createSlice({\n  name: \'testMap\',\n  // Using a Map() as redux state\n  initialState: new Map(),\n  reducers: {\n    add: (state, action) => {\n      state.set(action.payload.identity, action.payload)\n    },\n  },\n})\n
Run Code Online (Sandbox Code Playgroud)\n

但是,当我在 React 组件中使用它时,我收到了礼貌的错误消息A non-serializable value was detected in the state, in the path: `testMap`. Value: Map(1)\xc2\xa0{"A" => {\xe2\x80\xa6}} …

redux immer.js redux-toolkit

15
推荐指数
1
解决办法
8660
查看次数

标签 统计

immer.js ×1

redux ×1

redux-toolkit ×1