简体   繁体   English

如何在react中显示数组的数据?

[英]how to display the data of array in react?

http://codepen.io/naveennsit/pen/oxRwYM http://codepen.io/naveennsit/pen/oxRwYM

HI I am trying to display data of array in list. 您好,我想在列表中显示数组的数据。 I am getting undefined error why ? 我收到未定义的错误,为什么?

here is my code http://codepen.io/naveennsit/pen/oxRwYM 这是我的代码http://codepen.io/naveennsit/pen/oxRwYM

var stations = [
  {call:'station one',frequency:'000'},
  {call:'station two',frequency:'001'}
]; 
class App extends React.Component {

  render (){
    var stationComponents = this.props.name.map(function(station) {
            return <div className="station">{station.call}</div>;
        });
        return <div>{stationComponents}</div>;
  }
   handleClick(e){
     alert('--')
   } 

}


React.render(<App name='{stations}'>ssssss</App>,document.getElementById('app'))

You shouldn't wrap props in quotes, as that turns them into a string literal. 您不应该将道具用引号引起来,因为这会使它们变成字符串文字。 You're getting an undefined error because you're effectively trying to call map() on the string "{stations}" , rather than your stations object. 您收到未定义的错误,因为您实际上是在尝试对字符串"{stations}"而不是stations对象调用map()

Replace <App name='{stations}'>ssssss</App> with <App name={stations}>ssssss</App> . <App name='{stations}'>ssssss</App>替换为<App name={stations}>ssssss</App>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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