I'm a beginner in React, in the componentDidMount, I'm retrieving a response object from Axios then I'm setting it to the state object, even though it is accessible from outside functions it wouldn't work inside the render method, I don't know how to bind the state to get access to render()
error that pops: Unhandled Rejection (TypeError): Cannot read property 'imageref' of undefined
export default class Product extends Component {
constructor(props) {
super(props);
const id = this.state.comicId;
console.log("This is printed from the constructor " + id);
}
state = {
comicId: "",
issueObjectState: "",
};
render(){
<img
src={this.state.issueObjectState.imageref}
alt="Image Description"
className="mx-auto img-fluid"/>
}
async componentDidMount() {
let state = [];
const id = this.props.location;
let comicId = id.data;
this.setState({ comicId: this.props.comicId });
let issueData = await axios.get(`http://localhost:5000/comic/${comicId}`);
let comicData = issueData.data;
if (comicData) {
this.setState({ issueObjectState: comicData });
console.log(this.state.issueObjectState);
}
}
}
help is much appreciated
this.props.comicDatato theissueObjectState. Why is that?