我是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)