React.js this.props.data.map() is not a function React.js this.props.data.map()不是一个函数

9 浏览
0 Comments

React.js this.props.data.map() is not a function React.js this.props.data.map()不是一个函数

我正在使用React并尝试解析和渲染一个JSON对象。目前,我只是用一个硬编码的对象进行测试,而不是从ajax调用中获取数据。

var Person = React.createClass({

render: function() {

return (

);

}

});

var PersonDiv = React.createClass({

render: function() {

var personNodes = this.props.data.map(function(personData){

return (

personName={personData.person.firstname}

personSA1={personData.person.street1}

personSA2={personData.person.street2}

zip={personData.person.zip}

state={personData.person.state}

country={personData.person.country}>

)

});

return (

{personNodes}

);

}

});

React.render(

,

document.getElementById('jsonData')

);

我使用以下代码将data变量设置为一个字符串:


data对象是我在portlet的java端创建的。我知道JSON是有效的,因为我可以使用JSON.parse(json)来解析和遍历字符串,但我一直得到map()不是一个函数的错误。

0
0 Comments

问题原因:在React.render的第一个参数中传递了console.log的结果,导致出现了此错误。

解决方法:将console.log("inside render")移动到React.render之前,并将其作为独立的语句。

文章内容如下:

在使用React.js时,有时可能会遇到一个错误提示"React.js this.props.data.map() is not a function"。这个错误的出现是因为在React.render方法的第一个参数中传递了console.log的结果。

下面是一个代码示例:

React.render(
  console.log("inside render"),
  ,
  document.getElementById('jsonData')
);

要解决这个问题,我们需要将console.log语句移动到React.render之前,并将其作为独立的语句。修改后的代码如下:

console.log("will render");
React.render(
  ,
  document.getElementById('jsonData')
);

另外,在调试过程中,还可以在相同的位置添加console.log(data)语句来打印数据,以便更详细地了解问题所在。但是需要注意的是,将console.log语句放在render方法中也会导致错误的发生。

希望这篇文章能够帮助你解决React.js this.props.data.map() is not a function错误。

0
0 Comments

在使用React.js中,出现了一个错误提示为"React.js this.props.data.map() is not a function"的问题。该问题出现的原因是data不是一个json对象,而是一个字符串。解决该问题的方法是将data转换为一个实际的JavaScript对象,可以通过使用JSON.parse()方法来实现。可以通过以下代码进行测试:

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

运行以上代码后,应该能够注意到两者之间的差异。

0