如何使用不可变的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)