简体   繁体   English

API...在 React 问题中通过 componentDidMount 拉取 json.file 数据

[英]API...Pulling json.file data through with componentDidMount in React issues

Morning all I am trying to use componentDidMount lifecycle method to pull some data through on the frontend from a .json file.早上好,我正在尝试使用componentDidMount生命周期方法在前端从.json文件中提取一些数据。 and push it into the state. I need help with the following并将其推送到 state。我需要以下方面的帮助

  1. The .json file is placed alongside the app.js in a folder. .json文件与app.js放在一个文件夹中。 I am not sure if I am calling the file correctly.我不确定我是否正确调用了文件。 flying.json .飞行.json

  2. I have listed the json data too.我也列出了 json 数据。 How do I begin to pull that data through.我如何开始提取这些数据。 (legs (腿

Thanks peeps谢谢偷看

Joe

  1. app.js应用程序.js
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import { CardList }  from './../card-list/card-list'
import STYLES from './App.scss';
import Header from './../Header';

class App extends React.Component {
  constructor() {
    super();
      this.state = {
      tester: []
    };
  }

  componentDidMount() {
    fetch('flying.json')
      .then (response =>  response.json())
      .then(users => this.setState({tester:users}))
  }

  render() {
    return (
      <div style={{backgroundColor:'grey'}}>
        <Header />
        <CardList tester={this.state.tester}/>
      </div>
    )
  }
}

export default App 
  1. the json file flying.json (note this is situated directly alongside app.js) json 文件 flying.json(请注意,它直接位于 app.js 旁边)
{
  "itineraries": [
    {
      "id": "it_1",
      "legs": [
        "leg_1",
        "leg_4"
      ],
      "price": "£35",
      "agent": "Wizzair.com",
      "agent_rating": 9.1
    },
    {
      "id": "it_2",
      "legs": [
        "leg_2",
        "leg_5"
      ],
      "price": "£115",
      "agent": "British Airways",
      "agent_rating": 9.3
    },
    {
      "id": "it_3",
      "legs": [
        "leg_3",
        "leg_6"
      ],
      "price": "£90",
      "agent": "Lufthansa",
      "agent_rating": 8.9
    },
    {
      "id": "it_4",
      "legs": [
        "leg_1",
        "leg_5"
      ],
      "price": "£105",
      "agent": "Trip.com",
      "agent_rating": 9.5
    },
    {
      "id": "it_5",
      "legs": [
        "leg_1",
        "leg_6"
      ],
      "price": "£195",
      "agent": "Trip.com",
      "agent_rating": 9.5
    },
    {
      "id": "it_6",
      "legs": [
        "leg_2",
        "leg_4"
      ],
      "price": "£93",
      "agent": "Kiwi.com",
      "agent_rating": 8.0
    },
    {
      "id": "it_7",
      "legs": [
        "leg_3",
        "leg_4"
      ],
      "price": "£42",
      "agent": "CheapFligths",
      "agent_rating": 10.0
    }
  ],
  "legs": [
    {
      "id": "leg_1",
      "departure_airport": "BUD",
      "arrival_airport": "LTN",
      "departure_time": "2020-10-31T15:35",
      "arrival_time": "2020-10-31T17:00",
      "stops": 0,
      "airline_name": "Wizz Air",
      "airline_id": "WZ",
      "duration_mins": 145
    },
    {
      "id": "leg_2",
      "departure_airport": "BUD",
      "arrival_airport": "LHR",
      "departure_time": "2020-10-31T12:05",
      "arrival_time": "2020-10-31T17:00",
      "stops": 1,
      "airline_name": "British Airways",
      "airline_id": "BA",
      "duration_mins": 190
    },
    {
      "id": "leg_3",
      "departure_airport": "BUD",
      "arrival_airport": "LGW",
      "departure_time": "2020-10-31T22:35",
      "arrival_time": "2020-11-01T00:10",
      "stops": 0,
      "airline_name": "Lufthansa",
      "airline_id": "LH",
      "duration_mins": 155
    },
    {
      "id": "leg_4",
      "departure_airport": "LTN",
      "arrival_airport": "BUD",
      "departure_time": "2020-11-11T19:45",
      "arrival_time": "2020-11-11T21:10",
      "stops": 0,
      "airline_name": "Wizz Air",
      "airline_id": "WZ",
      "duration_mins": 145
    },
    {
      "id": "leg_5",
      "departure_airport": "LHR",
      "arrival_airport": "BUD",
      "departure_time": "2020-11-11T11:25",
      "arrival_time": "2020-11-11T19:10",
      "stops": 1,
      "airline_name": "British Airways",
      "airline_id": "BA",
      "duration_mins": 190
    },
    {
      "id": "leg_6",
      "departure_airport": "LGW",
      "arrival_airport": "BUD",
      "departure_time": "2020-11-11T08:10",
      "arrival_time": "2020-11-11T11:40",
      "stops": 0,
      "airline_name": "Lufthansa",
      "airline_id": "LH",
      "duration_mins": 150
    }
  ]
}

You can import it.你可以导入它。 eg例如

import data from "./flying.json";

Full example完整示例

import "./styles.css";
import data from "./flying.json";

export default function App() {
  return (
    <div className="App">
      {data.itineraries.map((item) => (
        <div key={item.id}>{item.id}</div>
      ))}
    </div>
  );
}

Working Example工作示例

https://codesandbox.io/s/funny-margulis-rdkxo?file=/src/App.js https://codesandbox.io/s/funny-margulis-rdkxo?file=/src/App.js

Another interesting option is to move the JSON file to the public directory and then use Javascript's fetch API to retrieve it.另一个有趣的选择是将 JSON 文件移动到公共目录,然后使用 Javascript 的 fetch API 来检索它。

import { useEffect, useState } from "react";

const App = () => {
  const [ data, setData ] = useState({});

  useEffect(() => {
    (async () => {
      await fetch("/flying.json")
      then(res => res.json())
      then(json => setData(json))
    })();
  });

  return(
    data.map(item => {
      return(
        <div key={item.id}>
          // return whatever you want here
        </div>
      )
    }
  }
}

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

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