Reputation: 1213
Using Immutable.fromJS()
on a raw JS object works as expected and returns a Map, but trying to do just the same Immutable.fromJS()
with a custom object obtained from a constructor function does not work.
Meaning:
{/*1*/}
: console.log
returns custom object.{/*2*/}
: console.log
returns the same custom object, even though I used Immutable.fromJS()
.{/*3*/}
: console.log
returns custom object.{/*4*/}
: console.log
returns map = Immutable.fromJS()
, as expected.I can't find any explanation in the documentation.
{/*EXPORTING CONSTRUCTOR*/}
export function MyObject() {
this.id = null;
this.name = '';
}
{/*EXPORTING RAW OBJECT*/}
export my_object = {
this.id = null;
this.name = '';
}
//in another file
//importing, and than:
let myConstructedObject = new MyObject();
let myRawObject = my_object;
{/*1*/} console.log(myConstructedObject) // => object
{/*2*/} console.log(Immutable.fromJS(myConstructedObject)) // => object ?!?!
{/*3*/} console.log(myRawObject) // => object
{/*4*/} console.log(Immutable.fromJS(myRawObject)) // => map (OK!)
Upvotes: 0
Views: 250
Reputation: 340
I think that people in this topic: How do I make a custom immutable type using facebook/immutable-js? actually discuss the same problem from a constructive point.
Summing up that discussion, you cannot just "Immutivize" a custom object, but you can write an immutable object on your own, using either Immutable.Record (with pure ImmutableJS) or Map (using 'extendable-immutable' library.)
Upvotes: 1