简体   繁体   English

React JS setState(…):只能更新已安装或正在安装的组件

[英]React JS setState(…): Can only update a mounted or mounting component

I am getting this error setState(...): Can only update a mounted or mounting component. 我收到此错误setState(...): Can only update a mounted or mounting component. but I can't work out how to fix it. 但我无法解决该问题。

import React, { Component } from 'react';

import Loading1 from '../images/loading1.gif';

class LoadingSpinner extends Component {
  constructor(props){
    super(props);

    this.changeState = this.changeState.bind(this);
    this.timer = this.timer.bind(this);
  }

  state = {
    loadingImg: Loading1,
    loading: true
  }

  timer(){
    var self = this;
    var startTime = new Date().getTime();
    var interval = setInterval(function () {
      if (new Date().getTime() - startTime > 3000) {
        clearInterval(interval);
        return;
      }
      self.changeState();
    }, 2000);
  }

  changeState(){
    this.setState({
      loading: false,
    })
  }

  render() {


    const topMargin = {
      marginTop: "50px"
    }


    return (
      <div className="containter" style={topMargin}>
        <center>
          {this.state.loading ? <img src={this.state.loadingImg} onLoad=    {this.timer()} alt="Loading..." /> : <h2>Unable To Find What You Are Looking     For!</h2> }
        </center>
      </div>
    );
  }
}

export default LoadingSpinner;

this is my code that is causing the issue. 这是导致问题的我的代码。

Basically I want it so that after the set amount of time it will change from the loading1.gif to say Unable to find what you are looking for. 基本上,我想要这样,以便在设置的时间后它将从loading1.gif更改为“无法找到您要查找的内容”。 It does this but it throws the error setState(...): Can only update a mounted or mounting component. 它会执行此操作,但是会引发错误setState(...): Can only update a mounted or mounting component. which I can't get rid of. 这是我无法摆脱的。

This is how I am calling loading spinner 这就是我所说的加载微调框

<Tab label="Applicant Details" value="GetCasePersonal">
     {this.state.GetCasePersonal.length === 0 ? <LoadingSpinner /> :
     <div>
         <ViewPersonalDetails Case={this.state.GetCasePersonal} />
     </div>
     }
</Tab>

You need not use a setInterval function, you can do it with setTimeout easily and you should use a React lifecycle function to have a timeout instead of calling it onLoad of image. 您不需要使用setInterval函数,可以轻松地使用setTimeout进行操作,并且应该使用React生命周期函数来设置超时,而不是将其调用为onLoad。

 class LoadingSpinner extends React.Component { constructor(props){ super(props); this.timeout = null; this.changeState = this.changeState.bind(this); } state = { loadingImg: '', loading: true } componentDidMount(){ this.timeout = setTimeout( () => { console.log('I am changing state'); this.changeState(); }, 3000); } componentWillUnmount() { clearTimeout(this.timeout); } changeState(){ this.setState({ loading: false, }) } render() { const topMargin = { marginTop: "50px" } return ( <div className="containter" style={topMargin}> <center> {this.state.loading ? <img src={this.state.loadingImg} alt="Loading..." /> : <h2>Unable To Find What You Are Looking For!</h2> } </center> </div> ); } } ReactDOM.render(<LoadingSpinner/>, document.getElementById('app')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

There's good documentation about this here https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html 这里有关于此的很好的文档https://facebook.github.io/react/blog/2015/12/16/ismount-antipattern.html

When dealing with such problems a good practice is to search in react facebook documentation. 处理此类问题时,一个好的做法是搜索react facebook文档。

private _isMount = false;

componentDidMount() {
  this._isMount = true;
}

componentWillUnmount()
{
    this._isMount = false;
}

timerJob()
{
    if(this._isMount == false)
        return;
    // setState
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React setState只能更新已安装或安装的组件 - React setState can only update a mounted or mounting component React setstate只能在重新渲染时更新已安装或安装的组件 - React setstate can only update a mounted or mounting component — on rerender React-setState(…)只能更新已安装或正在安装的组件 - React - setState(…) Can only update a mounted or mounting component React-setState(…):只能更新已安装或正在安装的组件 - React - setState(…): Can only update a mounted or mounting component 反应警告:setState(…)只能更新已安装或正在安装的组件 - React Warning: setState(…) Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件 - Warning react : setState(…): Can only update a mounted or mounting component React:setState只能更新已安装或安装的组件 - React: setState can only update a mounted or mounting component 反应警告setState(…):只能更新已安装或正在安装的组件 - React warning setState(…): Can only update a mounted or mounting component setState只能更新已安装或安装的组件 - setState Can only update a mounted or mounting component setState(…):只能更新已安装或正在安装的组件 - setState(…): Can only update a mounted or mounting component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM