I am building a little search engine and got following problem:
Everytime I enter a query, the last letter is missing. I figured out, that it has something to do with setState and that it is not asynchronous... But I can not come up with a solution fo my case.
Her is my function:
searchHandler = ({ target: { value } }) => {
this.setState({ term: value });
this.updateMessage(value);
if(this.state.message.length === 0){
this.setState({hasMoreItems: false})
this.componentDidMount();
}else{
var requestUrl = 'https://questdb.herokuapp.com/all?q='
fetch(requestUrl + this.state.message).then((response)=>{
return response.json();
}) .then((data)=>{
this.setState({ tracks: data});
})
}
}
updateMessage = message => this.setState({ message });
Do you have any suggestions for me?
Thanks a lot!