How to update value in a nested Immutable map

2k Views Asked by At

I am new to immutable.js and trying to figure out a way to update a nested map

Here is my object

let state = OrderedMap({
    'name': Map({ id: 'name', hint: 'Search by name', value: '' }),
    'job': Map({ id: 'job', hint: 'Search by job title', value: ''}),
    'state':  Map({ id: 'state', hint: 'Search by state', value: ''})
});

I am trying to set value of the 'name' object using a setIn function

state.setIn(['name', 'value'], 'Test');

The value is not getting updated as expected. Am I missing anything here

3

There are 3 best solutions below

0
On BEST ANSWER

setIn doesn't mutate the original state, however it returns you another one so you would essentially have

state = state.setIn(['name', 'value'], 'Test');
0
On

I've never used immutable, but it looks to me like the docs are saying you want something more like:

setIn(state, ['name', 'value'], 'Test');

Where you are passing in the collection rather than running a function on it with a dot operator.

https://facebook.github.io/immutable-js/docs/#/setIn

0
On

Instead of mutating the state you can use setIn as this will return you a fresh copy instead , So you can do something like :

state = state.setIn(['name', 'value'], 'Test');

You can read more on Facebook Github