const [book, ...rest] = this.state.details;
console.log(JSON.stringify(book, null, 2));
logs:
{
genre: [
{
_id: "5ad0ecf98c1cff1e849266f3",
name: "Fantasy",
url: "/catalog/genre/5ad0ecf98c1cff1e849266f3",
id: "5ad0ecf98c1cff1e849266f3"
}
],
_id: "5ad0ecfc8c1cff1e849266f7",
title: "The Wise Man's Fear (The Kingkiller Chronicle, #2)",
summary:
"Picking up the tale of Kvothe Kingkiller once again, we follow him into exile, into political intrigue, courtship, adventure, love and magic... and further along the path that has turned Kvothe, the mightiest magician of his age, a legend in his own time, into Kote, the unassuming pub landlord.",
author: {
_id: "5ad0ecf98c1cff1e849266ee",
first_name: "Patrick",
family_name: "Rothfuss",
date_of_birth: "1973-06-06T00:00:00.000Z",
name: "Rothfuss, Patrick",
url: "/catalog/author/5ad0ecf98c1cff1e849266ee",
id: "5ad0ecf98c1cff1e849266ee"
},
isbn: "9788401352836",
url: "/catalog/book/5ad0ecfc8c1cff1e849266f7",
id: "5ad0ecfc8c1cff1e849266f7"
};
I want to render:
render() {
const [book={}, ...rest] = this.state.details;
console.log(JSON.stringify(book, null, 2));
// console.log(book.title);
return (
<div>
<h2>{book.title}</h2>
<p>{book.author.name}</p>
</div>
);
}
getting error: Cannot read property 'title' of undefined, if I set book's default value to empty object i am able to get title, but now i get error: Cannot read property 'name' of undefined. What's the
here's the default state
state = { details: [], loading: true };
componentDidMount() {
fetch(this.props.match.url)
.then(res => res.json())
.then(data => this.setState({ details: data, loading: false }))
.catch(err => console.log(err));
}
rendermethodconsole.log(book.title);this.state.details