I'm creating a search page component, that contains a number of inputs to filter the search results.
Each input changes the state of the component, and should trigger an AJAX call to refresh the search results:
handleSearchStringChange(event) {
this.setState({
filterSearchString: event.target.value
});
loadSearchResults(event.target.value);
}
render() {
return (
// ...
<input type="text" value={this.state.filterSearchString} onChange={this.handleSearchStringChange} />
// ...
);
}
This is all good, however, as soon as you add more filters, it becomes cumbersome to pass all the filter values to loadSearchResults().
What I'd like to do, is update the state of the component using setState() as I do above, and then have loadSearchResults() build the AJAX request from the state alone.
I can't do this right now, because setState() is asynchronous, and doing so would make loadSearchResults() use potentially outdated data.
Is there any way to trigger the AJAX call after setState() has completed?