Using Immutable.js you get to know its function set quite quickly in the beginning. Set completely overwrites a property but what if we want to just set a property inside that property?
1 2 3 4 5 6 7 8 9 10 11 |
{ name: 'John', children: { 1: { name: 'Lisa', } 2: { name: 'Anna', }, }, } |
So imagine we have a object like that. We can simply use Immutable.set to change John to something else with
1 |
state.set('name', 'Richard') |
But if we want to change name of one of John’s children? Then we have to go deeper into the tree with Immutable.setIn:
1 |
state.setIn(['children', 1, 'name'], 'Jennifer') |
Notice the array syntax!
This way we can set as deep value as we want and we don’t have to completely overwrite the children object.