Immutablejs Map setIn: Plain Javascript object Error: invalid keyPath

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容