Search Filter Component Flashcards
1
Q
What is the basic react framework for SearchFilter?
A
const SearchFilter = () => { return ( <div> Search: </div> )}
export default SearchFilter;
2
Q
Create the handleSearch function part 1
A
- Create dummy list array of variables
- Import useState from react
const [filterList, setFilterList] = useState(list);
const handleSearch = (event) => { if (event.target.value === “”) { setFilterList(list); return; }
3
Q
handleSearch function part 2 (filtered values)
A
const filteredValues = list.filter( (item) => item.toLowerCase().indexOf(event.target.value.toLowerCase() ) ==! -1 ); setFilterList(filterValues); };
4
Q
Return the filtered list
A
After the <div> in the return statement: {filterList && filterList.map((item, index) => ( <div>{item}</div> ))} </div>