azrahel
azrahel

Reputation: 1213

ImmutableJS not working with new custom object instance

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:

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

Answers (1)

Konstantin
Konstantin

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

Related Questions