javascript - How to display Json Data in ReactJs - Stack Overflow

I have this code where I am just trying to display a Json Data but it returns undefined var dataW = [{

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

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

Share Improve this question asked May 17, 2016 at 12:58 Vikram Anand BhushanVikram Anand Bhushan 4,89615 gold badges74 silver badges133 bronze badges 2
  • @TheReason dataW ?? – Vikram Anand Bhushan Commented May 17, 2016 at 13:05
  • 1 Javascript object Vs JSON – The Reason Commented May 17, 2016 at 13:11
Add a ment  | 

2 Answers 2

Reset to default 2

data is your ponent'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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745585531a4634515.html

相关推荐

  • javascript - How to display Json Data in ReactJs - Stack Overflow

    I have this code where I am just trying to display a Json Data but it returns undefined var dataW = [{

    20小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信