Search Filter
In this exercise, you are tasked with creating a simple Search Filter component that allows users to filter a list of items based on their search input.
Expectations:
Create an input field for users to type in their search query
Display the list of items and filter them based on the user's search input
useState
hook to manage the search input stateSolution Walkthrough for Search Filter
Spoiler Alert!
Don't read this unless you've already solved the problem.
filteredItems
that filters the original items
array using the Array.prototype.filter()
method. We check if each item in the array includes the search term, ignoring case:const filteredItems = items.filter(item =>
item.toLowerCase().includes(search.toLowerCase())
);
This approach efficiently filters the list of items based on the user's search input, allowing us to display only the items that match the search criteria.
Array.prototype.map()
method to iterate through the filteredItems
array and create a list item for each element. This way, we dynamically display the filtered list of items:<ul>
{filteredItems.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
SearchFilter
component combines the concepts of controlled components and state management with the filtering functionality to create a responsive search experience for users. As the user types in the search input, the list of items is updated in real-time, reflecting the filtered results based on the user's input.