I am retrieving JSON data from an API and then storing it in state and when I try to access the data using this.state (eg i write this.state.DetailData.title) it works fine but when I try to access the nested data (eg this.state.DetailData.location.address) of the same object it throws me an error:
TypeError: Cannot read property 'address' of undefined
Here is my code:
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" sm = {{size: 8, offset: 2 } md = {{size: 8, offset: 2}} >
< div >
<h1> {this.state.DetailData.title} <h1>
<h1> {this.state.DetailData.location.address} </h1> {/*getting error here*/}
<hr/>
<h6> {this.state.DetailData.description} </h6>
</div>
</Col>
</Row>
</Container>
</div>
);
}
}
export default Detail;
location.addressis exists or not ????