Итак, код прост:
var timeout
this.fetchingData = (url) => {
timeout = setTimeout(
() => {
fetch(url)
.then(response => {
if (response.status !== 200) {
return console.error('Not working')
};
return response.json()
})
.then(data => {
this.setState({
Search: data.Search,
response: data.response,
totalResults: data.totalResults
})
})
.catch(error => {
alert(error)
})
}, timeToDelay);
};
this.myStopFunction = () => {
clearTimeout(timeout);
}
}
handlePageChange = (event) => {
if (this.state.movieName === '') {
console.error("ENTER MOVIE NAME FIRST")
} else {
this.myStopFunction()
this.handleChange(event)
}
}
handleChange = (event) => {
this.myStopFunction()
const {
value,
name
} = event.target;
this.setState({
[name]: value
}, () => value ? this.myFunction(this.state, this.url, this.fetchingData) : null);
};
внутри return()
<FormInput
name='page'
type='number'
handleChange={this.handlePageChange}
value={this.state.page}
label='page number'
required
/>