简体   繁体   中英

Immutable.js updateIn does not work with plain objects?

Docs state that it should work:

Plain JavaScript Object or Arrays may be nested within an Immutable.js Collection, and updateIn() can update those values as well, treating them immutably by creating new copies of those values with the changes applied.

However, the following code does not work:

import { Map } from 'immutable';
let m = new Map({a: {b: 5}})
m = m.updateIn(["a", "b"], x => x + 1);
console.log(m);

With exception Error: invalid keyPath . Same code with fromJS instead of new Map works.

Am I misunderstanding the docs or why is my code not working?

Edit: seems that this feature is new to 4.0.0, which is not installed by default.

I am using Immutable 4.0.0-rc.9

Well, it works here in the console. You might want to consider trying to import like this import Immutable from 'immutable' ; May be the ES6 Map and the import { Map } are clashing. I am not sure, please correct me if that is not the case.

 let m = Immutable.Map({a: {b: 5}}) m = m.updateIn(["a", "b"], x => x + 1); console.log(m); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM