The problem is that you are using a mutable variable inside an immutable list. The object does not change for the list, since oldObject === newObject is still true.
Here is a simplified example:
> var obj = {}; > var list = Immutable.List.of(obj); > list2 = list.update(0, function(obj) { obj.foo = 42; return obj;}); > list2.get(0) Object { foo: 42 } > obj Object { foo: 42 } > list.get(0) === list2.get(0) true
To solve this problem, besides updating the map (if you want it), you need to either
- Clone an object when updating
- Use map instead of object
- Probably best in this case: write instead of object
Example:
var MyRecord = new Immutable.Record({id: null, name: '', selected: false}); var mapA = Immutable.fromJS({ listA: [ new MyRecord({ id: 1, name: 'Name A' }), new MyRecord({ id: 2, name: 'Name B' }) ] }); var mapB = mapA.updateIn(['listA', 1], function(record) { return record.set('selected', true); }); console.log(mapB.get('listA') === mapA.get('listA'));
source share