【发布时间】:2020-05-12 07:06:43
【问题描述】:
我从 API 获取数据的功能不起作用
import React, { Component } from 'react'
class Contact extends Component{
constructor(){
super()
this.state = {
data: []
}
this.fetchData = this.fetchData.bind(this)
}
fetchData(){
let base_url = 'https://jsonplaceholder.typicode.com/photos/'
fetch(base_url, {method:"GET"})
.then(response => response.json())
.then(json => {
this.setState({
data:json
})
})
console.log(this.state.data)
}
render(){
return(
<div>
<h1>Contact page{this.state.data.title}</h1>
</div>
)
}
}
export default Contact
【问题讨论】:
-
请添加更多详细信息,例如您收到的错误消息等
-
可能你必须使用地图功能,因为你的状态“数据”是数组
标签: javascript reactjs react-router fetch-api