不可变的JS:更新嵌套的动态对象

时间:2018-07-09 07:39:39

标签: redux immutable.js redux-immutable

我的初始状态是

export const initialState = fromJS({
  current: {},
  page: {
    loading: false,
    isFirstLoaded: false,
  },
  meta: {},
});

在我的化简器中,由于我为page定义了嵌套结构,

state.setIn(['page', 'loading'], true);

工作正常。

由于我尚未为currentmeta定义嵌套结构

state.setIn(['current', 'status'], 'done')

引发错误“无效的密钥路径”

当我记录状态时,

{
   current: {..with properties including status is there as object..},
   page: Map, // <--- why this alone being as Immutable Map,
   meta: {}, // <--- This is also being as plain object
}

1 个答案:

答案 0 :(得分:0)

根据以下文档,您应该可以使用mergeIn方法:http://facebook.github.io/immutable-js/docs/#/Map/mergeIn

希望有帮助!

相关问题