React.js this.props.data.map() is not a function

前端 未结 2 790
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 22:30

I\'m messing around with react and trying to parse and render a json object. Right now, I\'m just setting it with a hard-coded object for testing and not getting it from an

相关标签:
2条回答
  • 2020-12-19 23:01

    It appears that your data is not a json object, it is a string. You probably need to run data = JSON.parse(data); to convert your data into an actual javascript object to be able to use it. An easy test for this would be to run

    <script>
      var data = "[" + '<%=data%>' + "]";
      console.log(data);
      console.log(JSON.parse(data));
    </script>
    

    You should notice the difference.

    0 讨论(0)
  • 2020-12-19 23:03

    You are passing result of console.log as first parameter to React.render:

    React.render(
     console.log("inside render"),
     <PersonDiv data={data} />,
    document.getElementById('jsonData')
    );
    

    It should be like:

    console.log("will render");
    React.render(
         <PersonDiv data={data} />,
        document.getElementById('jsonData')
    );
    
    0 讨论(0)
提交回复
热议问题