我正在从 API 检索 JSON 数据,然后将其存储在状态中,当我尝试使用this.state(例如我写this.state.DetailData.title)访问数据时,它工作正常,但是当我尝试访问this.state.DetailData.location.address同一对象的嵌套数据(例如)时,它会抛出我一个错误:
类型错误:无法读取未定义的属性“地址”
这是我的代码:
import React from 'react'
import Caro from './Carousel'
import {
Container,
Row,
Col
} from 'reactstrap';
class Detail extends React.Component {
constructor(props)
{
super(props)
this.state = {
DetailData: []
}
}
componentDidMount() {
fetch('https://api.kloh.in/kloh/external/v1/activity/AID180917200010526C8LFJ4MEIE9CWGXZG4', {
method: 'GET'
})
.then((response) => response.json())
.then((data) => {
this.setState({
DetailData: data.response
})
console.log(typeof(this.state.DetailData));
})
}
render() {
return (
<div>
<Caro / >
<Container >
<Row >
<Col xs = "12" …Run Code Online (Sandbox Code Playgroud)