简体   繁体   English

如何使用 react.js 从 RESTAPI 获取数据

[英]How to fetch data from RESTAPI with react.js

The data in the API looks like this: API 中的数据如下所示:

[
  { "lon": "4,483792", "lat": "51,917029" },
  { "lon": "4,483792", "lat": "51,919029" },
  { "lon": "4,483892", "lat": "51,929029" },
  { "lon": "4,484892", "lat": "51,931029" },
  { "lon": "4,423892", "lat": "51,919400" },
  { "lon": "4,438892", "lat": "51,915322" },
  { "lon": "4,483092", "lat": "51,917500" },
  { "lon": "4,403892", "lat": "51,925029" }
]

How can i retrieve these data to my react app?如何将这些数据检索到我的 React 应用程序?

I have something like this right now (I know it's wrong):我现在有这样的事情(我知道这是错误的):

import React, { Component } from 'react';

class Test3 extends Component {
    state = {
        loading: true,
        coordinates: null,
    }

    async componentDidMount() {
        const url = "https://ttr.vsbbn.nl:4000/gps_history?team_id=1";
        const response = await fetch(url);
        const data = await response.json();
        this.setState({ coordinates: lon, loading: false });
    }

    render() {
        return(
            <div>
                {this.state.loading || !this.state.coordinates ? (
                    <div>loading...</div>
                ) : (
                    <div>
                        <div>{this.state.lon}</div>
                    </div>
                )}
            </div>
        )
    }
}

export default Test3;

You almost got the good code !你几乎得到了好的代码! As mentionned you need to set your state with the right data piece named data :如前所述,您需要使用名为data的正确数据段设置您的状态:

const data = await response.json();
this.setState({coordinates: data, loading: false });

Now you can iterate through your data with map() method现在您可以使用map()方法遍历数据

import React, { Component } from 'react';

class Test3 extends Component{
    state = {
        loading: true,
        coordinates: null,
    }

    async componentDidMount(){
        const url = "https://ttr.vsbbn.nl:4000/gps_history?team_id=1";
        const response = await fetch(url);
        const data = await response.json();
        this.setState({coordinates: data, loading: false });

    }

    render(){
        const { loading, coordinates } = this.state
        return(
            <div>
                {loading || !coordinates ? (
                    <div>loading...</div>
                ) : (
                    <div>
                        {coordinates.map((coordinate, index) => {
                             return (
                               <div key={index}>
                                 <p>Longitute: {coordinate.lon}</p>
                                 <p>Latitude: {coordinate.lat}</p>
                               </div>
                             )
                         })}
                    </div>
                )}

            </div>
        )
    }

}

export default Test3;

You have to use map() to fetch data from the array.您必须使用map()从数组中获取数据。

this.state.coordinates.map( (item, index) => {
   return <div>
             <h3>The first group {index}</h3>
             <li>lon: {item[index]['lon']</li>
             // Make same to all
          </div>
}

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

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