Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable.js - toJS Is Not A Function

I am getting the typeError when I try using this function.

As per the Immutable.js docs, I want to convert an obj into normal JS obj.

Here is the example:

import { Map as iMap,
         toJS } from "immutable";

    let anExample = iMap({
        a : "a",
        b : "b"
    });
    console.log( "anExample is...", toJS( anExample ) );

I do not get an error on Map, but I do on toJS. What am I doing wrong? Thanks,

like image 658
Kayote Avatar asked May 04 '16 16:05

Kayote


1 Answers

You do not need to import toJS, since it's found on the immutable object itself. Based on your example, you would go about it like so:

import { Map as iMap } from "immutable";

let anExample = iMap({
    a : "a",
    b : "b"
});
console.log( "anExample is...", anExample.toJS() );

Another way you could go about this is to import fromJS. fromJS is a "lazy" way of converting JS to immutable; however, it is not ideal if you need particular Immutable structures (like an OrderedSet rather than a List).

You would handle that like this:

import { fromJS } from "immutable";

let anExample = fromJS({
    a : "a",
    b : "b"
});
console.log( "anExample is...", anExample.toJS() );

Hope this helps and let me know if you have any questions!

like image 118
Dom Avatar answered Sep 20 '22 18:09

Dom