简体   繁体   English

ImmutableJS记录合并

[英]ImmutableJS Record merge

I have imported an immutable record and wanted to add a new property to that before using it as the default state for my application. 我已经导入了一个不可变的记录,并想在将其用作应用程序的默认状态之前向该属性添加一个新属性。 However, I cannot add the property at all despite trying merge , mergeDeep , mergeWith and mergeDeepWith . 但是,尽管尝试了mergemergeDeepmergeWithmergeDeepWith ,但我根本无法添加该属性。 It returns the calling Record in all instances. 它在所有实例中返回调用记录。 I decided to try merge after seeing this link . 看到此链接后,我决定尝试merge

The below code snippet simulates my problem 下面的代码片段模拟了我的问题

 a = Immutable.Record({a:1, b:2}) b = a() c = Immutable.Record({z:12}) d = c() e = b.merge(d) console.log(e.toJS()) e = b.mergeDeep(d) console.log(e.toJS()) e = b.mergeWith(d) console.log(e.toJS()) e = b.mergeDeepWith(d) console.log(e.toJS()) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script> 

This does not work with v3.8.2 and even with v4.0.0-rc-9 , in all cases, b is returned. 这在v3.8.2甚至v4.0.0-rc-9中都v4.0.0-rc-9 ,在所有情况下,都将返回b I am looking for a solution using 3.8.2 itself not necessarily with merge 我正在寻找使用3.8.2本身不一定与merge的解决方案

Not really experienced with immutable, so any help would be really appreciated. 对于可变性没有真正的经验,所以任何帮助将不胜感激。

Thanks in advance 提前致谢

Maps are an approximation of key-value objects so use those instead of Records which have a different paradigm of Factories "A record is similar to a JS object, but enforces a specific set of allowed string keys, and has default values.". 映射是键值对象的近似值,因此请使用那些替代工厂具有不同范例的记录,“记录类似于JS对象,但强制使用一组特定的允许的字符串键,并具有默认值。”

 const a = Immutable.Map({ a: 1, b: 2 }); // or Immutable.fromJS({ ... }) const b = Immutable.Map({ z:12 }); const c = a.merge(b); console.log(c); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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