简体   繁体   English

哪个是不可变的js中最好的.toJS()和.toJSON()?

[英]Which is Best .toJS() vs .toJSON() in immutable js?

I am new to React+Redux+Immutable js ,I am using toJS() to tranforming my Immutable Data structure to Native javascript form.But Recently I stumbleupon with this tweet 我是React + Redux + Immutable js的新手,我使用toJS()将我的不可变数据结构转换为原生javascript格式。但最近我用这条推文恍恍惚惚

Author of Immutable.Js Immutable.Js的作者

Tweet says .toJS() is very expensive So can i go with toJSON() https://facebook.github.io/immutable-js/docs/#/List/toJSON is it ok with perf issue Tweet说.toJS()非常昂贵所以我可以使用toJSON() https://facebook.github.io/immutable-js/docs/#/List/toJSON是否可以使用perf问题

UPDATE UPDATE

const initialState = fromJS({
   postArr:[]
});

    const mapStateToProps = (state) => {
        return{
            posts:state.allPosts.toJS()
        }
    };

Pls provide some example with my own Reducer 请用我自己的Reducer提供一些示例

How can i iterate this.props.posts without using to.js() 如何在不使用to.js()的情况下迭代this.props.posts

From what I understand from the documentation : 根据我从文档中理解的内容

  • toJSON() returns a shallowly copied version of the immutable object (not a JSON string , although the name might suggest otherwise), which should only be used to pass to JSON.stringify() (in other words: you shouldn't use it as a regular object in your code). toJSON()返回一个浅层复制的不可变对象版本(不是JSON 字符串 ,虽然名称可能另有建议),它只能用于传递给JSON.stringify() (换句话说:你不应该使用它作为代码中的常规对象)。

  • toJS() returns a deeply copied version of the immutable object, which you can subsequently use/modify in your code. toJS()返回一个深度复制的不可变对象版本,您可以随后在代码中使用/修改它。 Because it's a deeply copied version, it's more expensive to generate. 因为它是一个深度复制的版本,所以它的生成成本更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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