[英]Immutable.js - toJS Is Not A Function
I am getting the typeError when I try using this function. 我尝试使用此函数时收到typeError。
As per the Immutable.js docs, I want to convert an obj into normal JS obj. 根据Immutable.js文档,我想将obj转换为普通的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
. 我没有在Map
上收到错误,但我在toJS
上收到错误。 What am I doing wrong? 我究竟做错了什么? Thanks, 谢谢,
You do not need to import toJS
, since it's found on the immutable object itself. 您不需要导入toJS
,因为它在不可变对象本身上找到。 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
导入。 fromJS
is a "lazy" way of converting JS to immutable; fromJS
是一种将JS转换为不可变的“懒惰”方式; however, it is not ideal if you need particular Immutable structures (like an OrderedSet
rather than a List
). 但是,如果您需要特定的不可变结构(如OrderedSet
而不是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! 希望这有帮助,如果您有任何疑问,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.