Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an object on page?

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?

like image 612
ajmajmajma Avatar asked Oct 16 '15 20:10

ajmajmajma


People also ask

How do I print an object in console?

Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.


2 Answers

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.

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')); 
like image 107
noveyak Avatar answered Sep 21 '22 05:09

noveyak


As a function component with JSON.stringify additional arguments for indentation

import React from 'react';  var person = {   firstName: "John",   lastName: "Doe",   age: 50,   eyeColor: "blue" };  const StringifyObject = () => (   <pre>     {JSON.stringify(person, null, 2)}   </pre> ) 
like image 21
Damien Avatar answered Sep 21 '22 05:09

Damien