1

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

2

2 Answers 2

2

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

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

jsfiddle.net/69z2wepo/42338 I changed but it doesnt show the value of the key instead its showing just the name
The problem is your data. What exactly are you trying to display?
well in my Localhost I have an API which I call to update the data , the Json looks like this [{"empAverage":3,"reviewerAverage":0,"stdAverageOfTask":5}] and I am trying to show in a table format thats all
0

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

3 Comments

jsfiddle.net/69z2wepo/42338 I changed but it doesnt show the value of the key instead its showing just the name
in server I am getting error , I am loading the Json from an API and it looks like this [{"empAverage":3,"reviewerAverage":0,"stdAverageOfTask":5}]
yes it shows error that SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.