【问题标题】:not getting any output while mapping an array of objects and displaying table rows in React JS在 React JS 中映射对象数组和显示表行时没有得到任何输出
【发布时间】:2018-10-02 01:13:43
【问题描述】:

我有一个对象数组,我想在表格中显示它的值 这就是我的数组的样子:

[{name: 'x', mobile: 'xxx'}, {name: 'y', mobile: 'yyy'}, ......]

我想在表格中显示它。 这是我到目前为止尝试过的

import React, { Component } from 'react';
import {
    Table,
    ProgressBar
} 
from 'react-bootstrap';

class Display extends Component {

  render() {
    var records = this.props.googleData;
    const API = this.props.api;
    const placeURI = this.props.placeURI;
    var rows = [];
    for(let p_id of records.results){
        let dataURI = `${placeURI}${p_id.place_id}${API}`;
        let proxyUrl = 'https://cors-anywhere.herokuapp.com/',
        targetUrl = dataURI
        fetch(proxyUrl + targetUrl)
        .then((res) => res.json())
        .then((data) => {
            let jsonData = JSON.parse(JSON.stringify(data));
            //console.log(jsonData);
            rows.push(jsonData.result);
        })
        .catch((e)=> console.log(`Error! ${e.message}`));
    }
    console.log(rows);

    return (
        <div>
            <ProgressBar now={45} />
            <Table striped bordered condensed hover responsive>
              <thead>
                <tr>
                  <th>#</th>
                  <th>Name</th>
                  <th>Full Address</th>
                  <th>Phone Number</th>
                  <th>International P.no</th>
                  <th>Website</th>
                  <th>Rating</th>
                </tr>
              </thead>
              <tbody>
                {rows.map(( listValue, index ) => {
                  return (
                    <tr key={index}>
                      <td>{listValue.name}</td>
                      <td>{listValue.title}</td>
                      <td>{listValue.price}</td>
                    </tr>
                  );
                })}
              </tbody>
            </Table>
        </div>
    );
  }
}

export default Display;

这就是我的数组的样子

但是map() 没有返回任何行。如果有任何建议可以改进我的代码,那是非常可观的。请帮忙

【问题讨论】:

    标签: arrays reactjs loops ecmascript-6


    【解决方案1】:
        import React, { Component } from 'react';
        import {
            Table,
            ProgressBar
        } 
        from 'react-bootstrap';
    
        class Display extends Component {
            constructor(props) {
                super(props);
                this.state={
                    rows: []
                }
            }
    
    
        componentDidMount = () => {
            var records = this.props.googleData;
            const API = this.props.api;
            const placeURI = this.props.placeURI;
            var rows = [];
            for (let p_id of records.results) {
                let dataURI = `${placeURI}${p_id.place_id}${API}`;
                let proxyUrl = 'https://cors-anywhere.herokuapp.com/',
                    targetUrl = dataURI
                fetch(proxyUrl + targetUrl)
                    .then((res) => res.json())
                    .then((data) => {
                        let jsonData = JSON.parse(JSON.stringify(data));
                        //console.log(jsonData);
                        rows.push(jsonData.result);
                    })
                    .catch((e) => console.log(`Error! ${e.message}`));
            }
            this.setState({
                rows:rows
            })
            console.log(rows);
        };
    
          render() {
    
        return (
            <div>
                <ProgressBar now={45} />
                <Table striped bordered condensed hover responsive>
                  <thead>
                    <tr>
                      <th>#</th>
                      <th>Name</th>
                      <th>Full Address</th>
                      <th>Phone Number</th>
                      <th>International P.no</th>
                      <th>Website</th>
                      <th>Rating</th>
                    </tr>
                  </thead>
                  <tbody>
                    {this.state.rows.map(( listValue, index ) => {
                      return (
                        <tr key={index}>
                          <td>{listValue.name}</td>
                          <td>{listValue.title}</td>
                          <td>{listValue.price}</td>
                        </tr>
                      );
                    })}
                  </tbody>
                </Table>
            </div>
        );
      }
    
        }
    
        export default Display;
    

    【讨论】:

    • 我正在添加我的数组的图像
    • @Waeez 你能不能在 render 方法中做 console.log(this.state.rows) 并告诉我结果。
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2023-01-07
    • 2021-10-12
    • 2023-03-03
    • 2017-04-22
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多