我已经尝试了各种不同的方法,但我被难住了。在 React 中使用 promise 和进行 api 调用的新手。这就是我目前所拥有的:
import React, { Component } from 'react'
import Column from './Column'
import { CardGroup } from 'reactstrap';
let api = "https://fcc-weather-api.glitch.me/api/current?";
class App extends Component {
constructor(props) {
super(props)
this.state = {
isLoaded: false,
items: {},
}
this.fetchWeather = this.fetchWeather.bind(this)
}
fetchWeather(apiStr) {
fetch(apiStr)
.then(res => res.json())
.then(
(result) => {
console.log(result)
this.setState({
isLoaded: true,
items: result.main
});
console.log(this.state);
},
// Note: it's important to handle errors here
// instead of a catch() …Run Code Online (Sandbox Code Playgroud)