哪个是redux中JSON对象类型的最佳Immutable JS数据结构。

时间:2017-08-21 08:15:45

标签: json reactjs data-structures redux immutable.js

我在 redux 中使用spread operators来排列JSON Object格式的数据,如下所示:

文件名: redux.js

export default function appData(state = {}, action) {
  switch (action.type) {
    case 'ADD_LAYERS':
    return {
      ...state, appElements: action.data.appElements
    }

    case 'ADD_ITEMS':
    return {
        ...state,appElements:{ ...state.appElements,
        layers: { ...state.appElements.layers,
          ['layer_1']:{ ...state.appElements.layers.layer_1,
            ['items']:{ ...state.appElements.layers.layer_1.items,
              [action.objData.item_id]:{
                "width":action.objData.width,
                "x":action.objData.x
              },
            }
          },
        }
      }
    }
    default:
      return state
  }
}

case 'ADD_LAYERS':的{​​{1}}将添加:(来自外部json文件的数据存储为对象键值对)

action.data.appElements

layers: { layer_1: { background: { width: '100px', height: '100px', bgColor: '#aaaaaa', bgImage: 'http:bgimage1.png' }, items: { } }, layer_2: { background: { width: '200px', height: '200px', bgColor: '#bbbbbb', bgImage: 'http:bgimage2.png' }, items: { } }, layer_3: { background: { width: '300px', height: '300px', bgColor: '#cccccc', bgImage: 'http:bgimage3.png' }, items: { } } } 将添加case 'ADD_ITEMS':项:

layer_1

最终的redux商店将如下所示:

yrgroih9: {
  width: '100px',
  x: '200px'
}

我希望从{ appElements: { layers: { layer_1: { background: { width: '100px', height: '100px', bgColor: '#aaaaaa', bgImage: 'http:bgimage1.png' }, items: { yrgroih9: { width: '100px', x: '200px' }, qhy0dukj: { width: '100px', x: '200px' }, '7lw2nvma': { width: '100px', x: '200px' } } }, layer_2: { background: { width: '200px', height: '200px', bgColor: '#bbbbbb', bgImage: 'http:bgimage2.png' }, items: { sdfsdsvod: { width: '555px', x: '111px' }, sdfcasadf: { width: '333px', x: '222px' }, 'xcvxcver': { width: '555px', x: '444px' } } }, layer_3: { background: { width: '300px', height: '300px', bgColor: '#cccccc', bgImage: 'http:bgimage3.png' }, items: { dfsgdfgfg: { width: '555px', x: '111px' }, sfgdyrsdf: { width: '333px', x: '222px' }, 'dsfdhsfdf': { width: '777px', x: '444px' } } } } } } 转到Immutable Js以获得更好的功能,例如中的redux spread operatorscreatereadupdate redux商店数据

哪个是最好的Immutable Js数据结构(列表,堆栈,地图,OrderedMap,Set,OrderedSet和Record)适合上面给出的商店操作 在结构和性能方面......?

1 个答案:

答案 0 :(得分:0)

对于当前状态对象,最自然的数据结构是Map,除非您需要特定的项目顺序,在这种情况下,请使用OrderedMap