简体   繁体   English

如何遍历on响应列表?

[英]How can I loop through a list of on react?

I've tried everything for what ever reason I can't return the current item in a list. 我已经尝试了一切,无论出于什么原因我都无法将当前项目退还到列表中。 when I do console log it shows theres a property inside of it but it does not render. 当我执行控制台日志时,它显示里面有一个属性,但不呈现。 What am I doing wrong? 我究竟做错了什么?

This is the data I'm getting from the parent component: 这是我从父组件获取的数据:

[
    {
        id: 3,
        sku: "3008_Brown",
        parent_sku: "3008",
        name: "Leonardo",
        description: "Frame with light & thin Rx lenses, UV & Scratch coatings, and polished edges",
        product_type_id: 1,
        gender_id: 3,
        kids: 0,
        price: "49.00",
        retail_price: "200.00",
        is_active: 1,
        mfr: "CAC",
        mfr_model: null,
        mfr_color: "GREEN",
        meas_a: 55,
        meas_b: null,
        meas_dbl: 17,
        temple_length: 140,
        total_frame_width: 142,
        spring_hinge: null,
        has_progressive: 0,
        created_at: null,
        updated_at: null
    }
]

This is the code: 这是代码:

class Result extends Component {
  constructor(props) {
    super(props);

    // set initial state
    this.state = {
        datas: '',
      users: [
                       {name: "John", id: 120, age: 22, gender: "male"},
                       {name: "Beth", id: 443, age: 24, gender: "female"},
                       {name: "Jane", id: 510, age: 19, gender: "female"}
                  ]
    };

    // binding 'this' to class
    this.displayList = this
      .displayList
      .bind(this);
  }

  // events we want to happen before it comes up
  componentWillMount(){
  }
  // events after load
  componentDidMount(){

  }

  displayList() {
      if (this.props.dataresults.length > 0) {
          this.props.dataresults.map(function(user, i){
              console.log('user');
              console.log(user.description);
             return <li key={i}>{user.description}</li>;
         })
     }  else {
         return (<h1>No Results </h1>)
     }
  }

  render() {
      console.log(this.state.users);
    return (
      <div class="search-results">
        <ul>


               {this.displayList()}
        </ul>
      </div>
    );
  }
}

You need to return the result of your map. 您需要返回地图结果。

  displayList() {
      if (this.props.dataresults.length > 0) {
          return this.props.dataresults.map(function(user, i){
              console.log('user');
              console.log(user.description);
             return <li key={i}>{user.description}</li>;
         })
     }  else {
         return (<h1>No Results </h1>)
     }
  }

As I mentioned in the comment, you need to return the result. 正如我在评论中提到的,您需要返回结果。

displayList() {
    if (this.props.dataresults.length > 0) {
         return this.props.dataresults.map(function(user, i){
               console.log('user');
               console.log(user.description);
               return <li key={i}>{user.description}</li>;
          })
     } else {
         return (<h1>No Results </h1>)
     }
}

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

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