I want to dynamic the URL with {this.state.whatever} in componentDidMount but failed. Because the state value is undefined(I run the console.log and the error also shows it)
I thought componentDidMount runs after the state reset, so I can use the updated state in the URL but doesn't work, I wonder why
state={ breed: "" }
async componentDidMount(){
try {
const res = await
fetch(`https://dog.ceo/api/breed/${this.state.breed}/images`);
const data = await res.json();
const imageUrl = data.message;
this.setState({
puppyImage: imageUrl
})
} catch (error) {
console.log(error);
}
}
onChangeHandler = event => {
this.setState({breed: event.target.value})
};
this URL doesn't work because of {this.state.breed} is undefined, which means it hasn't been updated.
the error shows:
GET https://dog.ceo/api/breed//images 404