繁体   English   中英

为什么Immutable.js记录中的数组是可变的?

[英]Why arrays in Immutable.js records are mutable?

我注意到在下面的代码中

const TaskRecord = new Immutable.Record({
  name: '',
  requiredFor: [],
});

class Task extends TaskRecord {
}

const task = new Task();

task.requiredFor是一个数组(在Task类中调用get('requiredFor')产生相同的结果),并且不会转换为Immutable.List ...这样的记录就不会不可变。

为什么会这样呢? 如何解决这个问题?

似乎在构造函数中制作魔术就可以了:

class Task extends TaskRecord {

   constructor(values) {
       super(Immutable.fromJS(values));
   }

}

暂无
暂无

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

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