Immutablejs Map setIn: Plain Jav

2017-12-18  本文已影响33人  kinkyboots

Immutable.js offical document of Map.setIn():

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

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

const { Map } = require('immutable@4.0.0-rc.9')
const originalMap = Map({
  subObject: {
    subKey: 'subvalue',
    subSubObject: {
      subSubKey: 'subSubValue'
    }
  }
})

originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
// Map {
//   "subObject": {
//     subKey: "ha ha!",
//     subSubObject: { subSubKey: "subSubValue" }
//   }
// }

Local code causes error:

const Immutable = require('immutable')
var map1 = Immutable.Map({ 'selector': { 'type': 'bar' }});
var map2 = map1.setIn(['selector', 'type'], 'foo');
console.log(map2.toJS());
node_modules/immutable/dist/immutable.js:870
    if (!condition) throw new Error(error);
                    ^
Error: invalid keyPath

Cause:

Map() function does not deeply convert JavaScript Objects and Arrays to Maps and Lists, use fromJS() instead.
https://stackoverflow.com/questions/37712871/why-does-immutable-js-throw-invalid-key-path-on-map-setin
https://stackoverflow.com/questions/39170433/immutable-js-getin-returns-undefined-but-works-in-console-from-the-browser

上一篇 下一篇

猜你喜欢

热点阅读