Based on an input from a user I am filtering over an array. It doesn't work if I use the teams from the state, but if I use the original array it does filter correctly. Can anyone explain why this is happening? I am a bit stumped.
There is a codesandbox here for reference.
const teams_data = [
"tottenham",
"arsenal",
"man utd",
"liverpool",
"chelsea",
"west ham"
];
function App() {
const [teams, setTeams] = React.useState(teams_data);
const [search, setSearch] = React.useState("");
return (
<div className="App">
<input
onChange={e => {
// if I filter on teams below it doesn't work as it should
// but if I use teams_data (original array) it works
const filteredTeams = teams_data.filter(team => {
return team.toLowerCase().includes(e.target.value.toLowerCase());
});
setTeams(filteredTeams);
setSearch(e.target.value);
}}
type="text"
value={search}
/>
{teams.map(team => (
<p>{team}</p>
))}
</div>
);
}