I made a simple search bar but the first time you type something in it you get
Cannot read properties of undefined (reading 'toLowerCase') TypeError: Cannot read properties of undefined (reading 'toLowerCase')
when i close the pop up and type the search bar works
function Search(event) {
setQuery(event.target.value);
if (event.target.value === '') {
setFilteredData(backendData);
} else {
setFilteredData(filteredData.filter(el => el.childName.toLowerCase().includes(query.toLowerCase())));
}
}
4
Answers
you can try this
It seems like the first time the
el.childName
orquery
are undefined.Could you try this?
Your filter data or query is undefined.
I don’t think you should set
query
andfilteredData
in the same function (that makesquery
state itself pointless). A more common approach is to use auseEffect
to change thefilteredData
accordingly: