I have a data array that holds all my users and shows them in a list. i want to create a search bar and filter the users based on the entered account number.
i tried to accomplish this by using the filter method, but when i do so, i get an error that my data array is no longer able to accept .map() function
here is my code:
const [data, setData] = useState([]);
const list = []
const filteredList = []
useEffect(() => {
firebase.firestore().collection("Users").get().then((userSnapshot) => {
userSnapshot.forEach((doc) => {
const {powerAccount,first_name,registerDate,email,company,country,phone} = doc.data();
setID(doc.data().usersID)
list.push({
usersID:doc.id,
powerAccount:powerAccount,
first_name:first_name,
registerDate:registerDate,
email:email,
company:company,
country:country,
phone:phone,
});
});
setData(list);
});
},[]);
function i am using in my searchfield input:
onChange={(e) => data.filter(account => e.target.value == account.powerAccount).map(filteredAccount => (
// console.log(filteredAccount)
setData(JSON.stringify(filteredAccount))
))}
in my return:
return (
{
after filtering the below code stops working. how can i fix this?
data.map((user,index) => (
<TableRow
hover
key={user.usersID}>
<TableCell>
<Box
sx={{
alignItems: 'center',
display: 'flex'
}}
>
<Typography
color="textPrimary"
variant="body1"
>
{user.powerAccount}
</Typography>
</Box>
</TableCell>
}
)
e.target.valueandaccount.powerAccountvia "==" and not "===" (the former does an implicit type coercion), or from the fact that you stringify your data, meaning you have a string now instead of an array. Most likely the second is why your code doesn't work anymore.setData(data.filter(account => e.target.value === account.powerAccount)).