简体   繁体   中英

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.

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));
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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