Answer the question
In order to leave comments, you need to log in
The state is not updated, what am I missing?
Hello!
The search only works the first time. Then, when the state should return after filtering, for some reason this does not happen.
Look, please, what could I have missed?
https://jsfiddle.net/anfc4qqj/
Thank you very much in advance!
Answer the question
In order to leave comments, you need to log in
The search only works the first time. Then, when the state should return after filtering, for some reason this does not happen.
class App extends React.Component {
state = {
people: this.props.people,
};
handeSearch = e => {
const search = e.target.value.toLowerCase();
this.setState((state, { people }) => ({
people: people.filter(n => n.name.toLowerCase().includes(search)),
}));
}
render() {
return (
<div className="contacts">
<input
type="text"
className="search-field"
onChange={this.handeSearch}
/>
<ul className="contacts-list">
{this.state.people.map(n => <Person key={n.id} {...n} />)}
</ul>
</div>
);
}
}
ReactDOM.render(
<App people={Peoples} />,
document.getElementById('root')
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question