无法使用不可变的js从redux商店添加对象中的新键

时间:2017-05-25 12:38:28

标签: javascript redux immutable.js

如何使用不可变的js在redux存储中向对象添加新键?例如。我的状态中有一个chartdata键,这是一个空的对象。我打电话来获取图表数据。调用完成后,我的reducer会得到一个chartId和一个data数组。我只想添加一个chartId的新密钥,并将值设置为data。因此,当我的电话继续进行时,chartdata将会显示如下:

chartdata: {
    "123" : [{}, {}],
    "324" : [{}, {}],
... } 

我尝试了以下代码:

return state.setIn(['chartData', action.chartId], action.data);

如果找不到它,则期望它创建一个新的keyId键,如果数据已经存在,则更新数据。

我在浏览器控制台中收到以下错误:

uncaught at getChartDataSaga Error: invalid keyPath
at invariant (webpack:///./~/immutable/dist/immutable.js?:872:27)
at updateInDeepMap (webpack:///./~/immutable/dist/immutable.js?:1976:5)
at updateInDeepMap (webpack:///./~/immutable/dist/immutable.js?:1982:23)
at Map.updateIn (webpack:///./~/immutable/dist/immutable.js?:1280:26)
at Map.setIn (webpack:///./~/immutable/dist/immutable.js?:1258:19)
at chartsReducer (eval at ./app/modules/Library/Charts/reducer.js (http://localhost:3000/14.chunk.js:15:1), <anonymous>:26:20)
at eval (webpack:///./~/redux-immutable/dist/combineReducers.js?:37:39)
at Array.forEach (native)
at eval (webpack:///./~/redux-immutable/dist/combineReducers.js?:34:25)
at Map.withMutations (webpack:///./~/immutable/dist/immutable.js?:1355:7)

0 个答案:

没有答案