I want to add components to dom through the array display function.
{books.map((book) => <Book/>)}
However, this doesn’t add anything, it looks like the array of books is empty? I printed books using {console.log(books)}, and it is not empty. What is wrong here? I added my render function.
render() {
let books = this.state.books;
return (<div className="bookshelf">
<h2 className="bookshelf-title">{this.titles[this.props.shelf]}</h2>
<div className="bookshelf-books">
<ol className="books-grid">
{books.map((book) => <Book/>)}
</ol>
</div>
</div>)
}
For any information, I get the values of the book through an api call.
componentWillMount() {
BooksAPI.getAll().then((books) => {
books.forEach((book) => {
if (this.state.shelf === book.shelf){
// console.log(book);
this.state.books.push(book);
}
})
})
}
Thank you if you understand what is happening.
source
share