I have searchInput in my app to search between an array of objects. I want to start searching when the user stops typing. Where is better to clear the timeout? and how?
this is my searchInput
:
<input
value={this.state.searchInput}
id="searchInput"
onChange={this.inputsChangedHandler}
/>
contacts
is my main state.
searchedContact
should display when the user stops typing.
inputsChangedHandler = (e) => {
let searched = this.state.contacts.filter((i) => {
return (
i.numbers.includes(e.target.value) ||
i.name.includes(e.target.value) ||
i.family.includes(e.target.value)
);
});
this.setState(
{
[e.target.id]: e.target.value,
},
() => {
setTimeout(() => {
this.setState({
searchedContact: searched,
});
}, 2000);
}
);
};