简体   繁体   English

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

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

I noticed that in the following piece of code 我注意到在下面的代码中

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

class Task extends TaskRecord {
}

const task = new Task();

task.requiredFor is really an array (calling get('requiredFor') inside the Task class yields the same result) and doesn't get converted to Immutable.List... this way the record isn't immutable. task.requiredFor是一个数组(在Task类中调用get('requiredFor')产生相同的结果),并且不会转换为Immutable.List ...这样的记录就不会不可变。

Why it is this way? 为什么会这样呢? How to fix this? 如何解决这个问题?

Seems like making magic in constructor does the job: 似乎在构造函数中制作魔术就可以了:

class Task extends TaskRecord {

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

}

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

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