Javascript 反应,在页面上打印对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33178780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:44:34  来源:igfitidea点击:

React, print object on page

javascriptreactjs

提问by ajmajmajma

I am have a big piece of data (list) stored in the state that I want to print out on my page in react.

我有一大段数据(列表)存储在我想在我的页面上打印出来的状态中以进行反应。

I have tried -

我试过了 -

 <div>{JSON.stringify(myObject)}</div>

and

 <div>{myObject.toString()}</div>

tostring does not work, but i thought i would give it a shot. I am unsure how to do this, I know if I was in angular I could store the object in a $scope variable, and just do {{myVar}} on the page to render the object. Is there any way to do this quickly in react? Thanks

tostring 不起作用,但我想我会试一试。我不确定如何做到这一点,我知道如果我在 angular 中,我可以将对象存储在 $scope 变量中,只需在页面上执行 {{myVar}} 来呈现对象。有什么办法可以快速做出反应吗?谢谢

回答by noveyak

I think your example should work. Not sure what kind of representation you are hoping for but I put an example on jsfiddle where I use JSON.stringify to print out an object.

我认为你的例子应该有效。不确定你希望什么样的表示,但我在 jsfiddle 上放了一个例子,我使用 JSON.stringify 打印出一个对象。

https://jsfiddle.net/5yq9fev6/1/

https://jsfiddle.net/5yq9fev6/1/

var world = {
    'abc' : [1, 2, 3],
    'b': {
        1: 'c'
     }
}

var Hello = React.createClass({
    render: function() {
        return (
            <div>        
                <div>{JSON.stringify(world)}</div>
            </div>
        )
    }
});

React.render(<Hello />, document.getElementById('container'));