代码之家  ›  专栏  ›  技术社区  ›  Kamalakannan J

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

  •  0
  • Kamalakannan J  · 技术社区  · 7 年前

    我的初始状态是,

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

    在我的reducer中,因为我已经为 page ,

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

    工作正常。

    因为我还没有为定义嵌套结构 current meta

    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 回复  |  直到 7 年前
        1
  •  0
  •   Aibu    7 年前

    你应该能够使用 mergeIn 方法,请参阅以下文档: http://facebook.github.io/immutable-js/docs/#/Map/mergeIn

    希望有帮助!