最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unmounting a Component with a SetInterval in React - Stack Overflow

programmeradmin1浏览0评论

I'm trying to unmount a component with a setInterval.

This is based on the answer here:

Component:

class ImageSlider extends React.Component {
  constructor(props) {
    super(props);
    this.state = { activeMediaIndex: 0 };
  }

  componentDidMount() {
    setInterval(this.changeActiveMedia.bind(this), 5000);
  }

  changeActiveMedia() {
    const mediaListLength = this.props.mediaList.length;
    let nextMediaIndex = this.state.activeMediaIndex + 1;

    if(nextMediaIndex >= mediaListLength) {
      nextMediaIndex = 0;
    }

    this.setState({ activeMediaIndex:nextMediaIndex });
  }

  renderSlideshow(){
    const singlePhoto = this.props.mediaList[this.state.activeMediaIndex];
      return(
        <div>
          <img src={singlePhoto.url} />
        </div>
      );
    }

  render(){   
    return(
      <div>
          {this.renderSlideshow()}
      </div>
    )
  }
}

Right now, when I go to another page, I get this error:

Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component

So I added something like this:

   componentWillUnmount(){
    clearInterval(this.interval);
  }

I've also tried:

   componentWillUnmount(){
    clearInterval(this.changeActiveMedia);
  }

But I'm still getting the above error every 5 seconds. Is there a proper way to clear the interval?

I'm trying to unmount a component with a setInterval.

This is based on the answer here:

Component:

class ImageSlider extends React.Component {
  constructor(props) {
    super(props);
    this.state = { activeMediaIndex: 0 };
  }

  componentDidMount() {
    setInterval(this.changeActiveMedia.bind(this), 5000);
  }

  changeActiveMedia() {
    const mediaListLength = this.props.mediaList.length;
    let nextMediaIndex = this.state.activeMediaIndex + 1;

    if(nextMediaIndex >= mediaListLength) {
      nextMediaIndex = 0;
    }

    this.setState({ activeMediaIndex:nextMediaIndex });
  }

  renderSlideshow(){
    const singlePhoto = this.props.mediaList[this.state.activeMediaIndex];
      return(
        <div>
          <img src={singlePhoto.url} />
        </div>
      );
    }

  render(){   
    return(
      <div>
          {this.renderSlideshow()}
      </div>
    )
  }
}

Right now, when I go to another page, I get this error:

Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component

So I added something like this:

   componentWillUnmount(){
    clearInterval(this.interval);
  }

I've also tried:

   componentWillUnmount(){
    clearInterval(this.changeActiveMedia);
  }

But I'm still getting the above error every 5 seconds. Is there a proper way to clear the interval?

Share Improve this question edited Mar 30, 2017 at 0:32 lost9123193 asked Mar 30, 2017 at 0:26 lost9123193lost9123193 11k27 gold badges83 silver badges125 bronze badges 1
  • setInterval returns an id which you later pass ti clearInterval on unmount. – jnes Commented Mar 30, 2017 at 0:47
Add a comment  | 

1 Answer 1

Reset to default 20

setInterval returns an in interval Id that you can use in clearInterval.

More info on setInterval

Something like this should work:

this.myInterval = setInterval(this.changeActiveMedia.bind(this), 5000)

then in componentWillUnmount:

clearInterval(this.myInterval);

发布评论

评论列表(0)

  1. 暂无评论