简体   繁体   中英

How to display Json Data in ReactJs

I have this code where I am just trying to display a Json Data but it returns undefined

var dataW = [
        {
       empAverage:3,
       reviewerAverage:4,
       stdAverageOfTask:1
    }
]

var ReviewCalculator = React.createClass({

                            getInitialState: function() {
                                      return {data: dataW};

                              },

                          render: function() {
                              return (
                                        <table>
                                                <tr><th>empAverage</th><td>{ this.props.data.empAverage}</td></tr>
                                        </table>
                                  );

                              }
                      });
 ReactDOM.render(<ReviewCalculator    />, document.getElementById('container'));

The Fiddle is here LINK

data is your component's 'state' set here:

getInitialState: function() {
 return {data: dataW};
}

Therefore change this.props.data.empAverage to this.state.data.empAverage .

Of course, because you dont have this.props.data.empAverage instead of that you are having this.state.data.empAverage & your data it's an array. For this example i've changed to [0] but for the future a suggest using Array.prototype.map method

Worked example

var dataW = [
        {
       empAverage:3,
       reviewerAverage:4,
       stdAverageOfTask:1
    }
]

var ReviewCalculator = React.createClass({
   getInitialState: function() {
     return {data: dataW};
   },
   render: function() {
     return (
        <table>
           <tr>
             <td>{this.state.data[0].reviewerAverage}</td>
           </tr>
        </table>
     );

   }
});
 ReactDOM.render(<ReviewCalculator/>, document.getElementById('container'));

You can event remove JSON.stringify if you want. fiddle

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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