相关疑难解决方法(0)

使用create-react-app获取API数据

我是Reactjs的新手,并且正在使用create-react-app入门,但是我不明白如何进行api调用来获取数据。这是我的代码:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
// import { URL, KEY, city, countryCode } from './config.json';


const KEY = "d7ba7d7818dd3cec9ace78f9ad55722e";
const URL = "api.openweathermap.org/data/2.5";
const CITY = "Paris";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {}
    };
  }

  componentDidMount() {
    var url = `${URL}/weather?q=${CITY}&APPID=${KEY}&units=metric`;
    console.log(url);
    fetch(url).then(
      response => response.json()
    ).then(
      json => {
        console.log(json);
        this.setState({data: json});
      }
    );
  }

  render() {
    return (
      <div className="App">
        <header className="App-header"> …
Run Code Online (Sandbox Code Playgroud)

javascript fetch reactjs create-react-app

5
推荐指数
1
解决办法
2846
查看次数

标签 统计

create-react-app ×1

fetch ×1

javascript ×1

reactjs ×1