I'm working on a react project to learn react.
In a component's render method, when I use .map
to iterate over values and return an array of components, everything works as expected.
<ol className=books-grid>
{
books && books.map((book, index) => {
if (book.shelf === shelf) {
return (
<Book key={book && book.id ? book.id : index} changeShelf={this.props.changeShelf} book={book} />
);
}
})}
</ol>
But when I use filter
:
<ol className=books-grid>
{
books && books.filter((book, index) => {
if (book.shelf === shelf) {
return (
<Book key={book && book.id ? book.id : index} changeShelf={this.props.changeShelf} book={book} />
);
}
})}
</ol>
I get the error (which I've researched)
Uncaught (in promise) Error: Objects are not valid as a React child
I don't understand why filter
is throwing this error vs map
? Is there something unique to react and .map
? Both return an array.