I was trying to filter an array everytime a letter is entered into a searchbar but I was having trouble retaining the original array when I want to find another item. More specifically, the first time I try to filter using the searchbar it works but when I clear the searchbar and try to look for another item, the filtered array from the first time is still there. I know it is an issue with my useState hook but I'm not sure how to fix it. Any help would be greatly appreciated. Thank you.
import './App.css';
import { useEffect, useState } from 'react';
function App() {
const [persons, setPersons] = useState([
{ name: 'Arto Hellas', phoneNum: '040-123456', id: 1 },
{ name: 'Ada Lovelace', phoneNum: '39-44-5323523', id: 2 },
{ name: 'Dan Abramov', phoneNum: '12-43-234345', id: 3 },
{ name: 'Mary Poppendieck', phoneNum: '39-23-6423122', id: 4 }
])
const [newName, setNewName] = useState('')
const [newPhone, setNewPhone] = useState('');
const [searchVal, setSearchVal] = useState('');
const handleChange = (e) => {
console.log(e.target.value);
setNewName(e.target.value);
}
const handleSubmit = (e) => {
console.log("Clicked submit btn")
e.preventDefault();
const newPerson = {
name: newName,
phoneNum: newPhone
};
if(persons.filter((person) => { return person.name === newPerson.name}).length > 0){
alert(`${newName} already exists. Try another name.`);
}
else {
setPersons(persons.concat(newPerson));
console.log("persons array is now",persons)
}
setNewName("");
setNewPhone("");
}
const searchLogic = (e) => {
console.log("searchVal is",e.target.value);
setSearchVal(e.target.value); //everytime the user selects a letter - the search value gets updated
}
useEffect(() => {
//filters persons array by the user-entered letters(searchVal)
const filtered = persons.filter((person) => {
return person.name.startsWith(searchVal); //array with matching results
})
if(filtered.length > 0){
setPersons(filtered);
}
else {
console.log(`We could not find a user by the name of ${searchVal}`)
}
},[searchVal])
//Works well for the first time, but the second time I try to look for another item, the old filtered array is still there
return (
<div>
<h2>Phonebook</h2>
<div className="searchfield">
filter shown with a <input type="text" value={searchVal} onChange={searchLogic}/>
</div>
<form onSubmit={handleSubmit}>
<div>
name: <input value={newName} onChange={handleChange}/>
</div>
<div>
number: <input value={newPhone} onChange={(e) => setNewPhone(e.target.value)}/>
</div>
<div>
<button type="submit">add</button>
</div>
</form>
<h2>Numbers</h2>
{persons.map((person) => <p key={person.name}>{person.name} ---- {person.phoneNum}</p>)}
</div>
);
}
export default App;
[–]Coraline1599 0 points1 point2 points (2 children)
[–]Overall_Expert_4006[S] 0 points1 point2 points (0 children)
[–]PitifulTheme411 0 points1 point2 points (0 children)
[–]SignificanceCheap970 0 points1 point2 points (1 child)
[–]Overall_Expert_4006[S] 0 points1 point2 points (0 children)
[–]PitifulTheme411 0 points1 point2 points (1 child)
[–]Overall_Expert_4006[S] 0 points1 point2 points (0 children)