Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deep update two values inside Immutable Js Map in redux?

I am trying to update two(width & x) values inside items -> yrgroih9 as given below:

{
  appElements: {
    layers: {
      layer_1: {
        background: {
          width: '100px',
          height: '100px',
          bgColor: '#aaaaaa',
          bgImage: 'http:bgimage1.png'
        },
        items: {
          yrgroih9: {
             width: '100px',
             x: '200px',
             y: '200px'
           },
           qhy0dukj: {
             width: '100px',
             x: '200px',
             y: '200px'
           },
           '7lw2nvma': {
             width: '100px',
             x: '200px',
             y: '200px'
           }
        }
      }
    }
  }
}

Code used to update new object inside items-> yrgroih9:

case 'UPDATE_OBJECT':
  return state.setIn(["appElements","layers","layer_1","items","yp57m359"],{
    ["width"]: action.objData.width,
    ["x"]: action.objData.x
  });

The above code removes y key inside the current location yrgroih9 and updates the width and x values.

Redux store data arranged after setIn: (from chrome redux devtools): enter image description here

How to update two deep values without removing the other key values.?

like image 663
ArunValaven Avatar asked Sep 11 '17 11:09

ArunValaven


People also ask

Is map immutable Javascript?

Persistent data presents a mutative API which does not update the data in-place, but instead always yields new updated data. Immutable. js provides many Persistent Immutable data structures including: List , Stack , Map , OrderedMap , Set , OrderedSet and Record .

What is immutable update?

Immutable environment updates are an alternative to rolling updates. Immutable environment updates ensure that configuration changes that require replacing instances are applied efficiently and safely. If an immutable environment update fails, the rollback process requires only terminating an Auto Scaling group.


1 Answers

Use updateIn.

If your items are instances of Immutable.js Map:

case 'UPDATE_OBJECT':
  return state.updateIn(['appElements', 'layers', 'layer_1', 'items', 'yrgroih9'],
    (item) => item
      .set('width', action.objData.width)
      .set('x', action.objData.x)
  );

If your items are plain JS objects:

case 'UPDATE_OBJECT':
  return state.updateIn(['appElements', 'layers', 'layer_1', 'items', 'yrgroih9'],
    (item) => ({
      ...item,
      width: action.objData.width,
      x: action.objData.x,
    })
  );
like image 156
quotesBro Avatar answered Nov 09 '22 10:11

quotesBro