I fetched the data like this:
constructor(){
super();
this.state = {
fetchedData : []
}
}
componentDidMount(){
fetch("https://api.randomuser.me")
.then(response => response.json())
.then(data => {
this.setState({
fetchedData : data.results[0]
})
});
}
Got this result with this.state.fetchedData
:
Then tried the following code to destructure the result:
render(){
let {name} = this.state.fetchedData;
It works fine and I got the following result with console.log(name)
:
So I wanted to further move down the nested object, but when I tried the following code:
render(){
let {name:{first, last, title}} = this.state.fetchedData;
It gave me the following error: