setState(...):只能更新已安装或安装的组件.这通常意味着您在已卸载的组件上调用了setState().这是一个无操作

use*_*ang 77 reactjs

componentDidMount(prevProps, prevState, prevContext) {
    let [audioNode, songLen] = [this.refs.audio, List.length-1];

    audioNode.addEventListener('ended', () => {
        this._endedPlay(songLen, () => {
            this._currSong(this.state.songIndex);
            this._Play(audioNode);
        });
    });

    audioNode.addEventListener('timeupdate', () => {
        let [remainTime, remainTimeMin, remainTimeSec, remainTimeInfo] = [];

        if(!isNaN(audioNode.duration)) {
            remainTime = audioNode.duration - audioNode.currentTime;
            remainTimeMin = parseInt(remainTime/60);  // ???
            remainTimeSec = parseInt(remainTime%60);  // ???

            if(remainTimeSec < 10) {
                remainTimeSec = '0'+remainTimeSec;
            }
            remainTimeInfo = remainTimeMin + ':' + remainTimeSec;
            this.setState({'time': remainTimeInfo});
        }
    });
}

componentWillUnmount () {
    let audio = this.refs.audio;
    audio.removeEventListener('timeupdate');
    audio.removeEventListener('ended');
}
Run Code Online (Sandbox Code Playgroud)

错误:

警告:setState(...):只能更新已安装或安装的组件.这通常意味着您在已卸载的组件上调用了setState().这是一个无操作.请检查未定义组件的代码.

我removeEventListener'已结束' componentWillUnmount,但它无效.因为我加this.setState({'time': remainTimeInfo});componentDidMount.

Tud*_*rar 97

我通过为组件分配ref然后在设置状态之前检查ref是否存在来解决这个问题:

myMethod(){
  if (this.refs.myRef) 
   this.setState({myVar: true});
}

render() {
  return (
    <div ref="myRef">
      {this.state.myVar}
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!然而,这是非常恶化的.几乎我的所有场景都基于promise进行加载,并抛出此错误.为什么React只能默默地拒绝在卸载状态下运行setState.这并不像任何人会看到差异. (10认同)

Bri*_*and 23

removeEventListener与...有相同的签名addEventListener.删除侦听器的所有参数必须完全相同.

var onEnded = () => {};
audioNode.addEventListener('ended', onEnded, false);

this.cleanup = () => {
  audioNode.removeEventListener('ended', onEnded, false);
}
Run Code Online (Sandbox Code Playgroud)

并在componentWillUnmount调用this.cleanup().


Sea*_*ene 5

我遇到这个问题是因为我在构造函数中使用了setState而不是state

例子

更改以下错误代码

constructor(props) {
  super(props);
  this.setState({
    key: ''
  });
}
Run Code Online (Sandbox Code Playgroud)

constructor(props) {
  super(props);
  this.state = {
    key: ''
  }; 
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*l B 3

编辑isMounted已弃用,可能会在 React 的更高版本中删除。看看这个和这个,isMounted 是一个 Antipattern


正如警告所述,您正在调用this.setState一个安装但此后已被卸载的组件。

为了确保您的代码安全,您可以将其包装在

if (this.isMounted()) {
    this.setState({'time': remainTimeInfo});
}
Run Code Online (Sandbox Code Playgroud)

以确保组件仍然安装。

  • this.isMounted 已被弃用,并且它不能解决事件侦听器泄漏的问题(这就是它被弃用的主要原因) (4认同)