React – setState() on unmounted component

Without seeing the render function is a bit tough. Although can already spot something you should do, every time you use an interval you got to clear it on unmount. So:

componentDidMount() {
    this.loadInterval = setInterval(this.loadSearches, this.props.pollInterval);

componentWillUnmount () {
    this.loadInterval && clearInterval(this.loadInterval);
    this.loadInterval = false;

Since those success and error callbacks might still get called after unmount, you can use the interval variable to check if it’s mounted.

this.loadInterval && this.setState({
    loading: false

Hope this helps, provide the render function if this doesn’t do the job.


Leave a Comment