内存泄漏,需要销毁
componentDidMount(){ this.timer=setInterval ( ()=>{ let opacity = this.state.opacity; opacity -= .05; if (opacity < 0.1) { opacity = 1.0; } this.setState({ opacity: opacity }); },100) } componentWillUnmount() { // 如果存在this.timer,则使用clearTimeout清空。 // 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear clearTimeout(this.timer); }