I have a search bar component. I would like to know how I can refactor the below code such that I can include html markup to display if there is no match. Right now the markup displays if there is a match or nothing displays if its blank.
I was getting errors when separating out the filter and map function.
return (
<div>
<input placeholder="Search Book Title" onChange={handleChange} />
{
bookList.filter(book => {
let isMatched = (book.title.toLowerCase().includes(query.toLowerCase()));
if (query === '') {
return ;
} else if (isMatched) {
return book;
}
}).map((book, index) => (
<div>
This is the book
</div>
))
}
</div>
)
};
I was getting errors when separating out the filter and map function.
2
Answers
Do the filter first and assign the result to a variable. Check if the .length is zero, if it is return your no books found markup. Otherwise return your books that were found list. For example
I would recommend assigning the result of the filtered books to a variable and then use it in your rendered result –
If the filter is an expensive computation, you should
useMemo
–