Jquery 如何删除“警告”;can';t对未安装的组件执行react状态更新“;
我有一个认证系统,用户登录,如果认证,然后重定向到主页用户。。。这是工作正常,但唯一的问题是,它是给我一个警告,不能更新状态的卸载组件我已经尝试了所有的方法,与此有关的问题在互联网上,但未能删除警告 我在componentDidMount中设置了isMounted的标志并将其设置为true,但没有帮助。。我还尝试在componentWillUnMount中清除间隔和超时,但仍然失败Jquery 如何删除“警告”;can';t对未安装的组件执行react状态更新“;,jquery,ajax,reactjs,Jquery,Ajax,Reactjs,我有一个认证系统,用户登录,如果认证,然后重定向到主页用户。。。这是工作正常,但唯一的问题是,它是给我一个警告,不能更新状态的卸载组件我已经尝试了所有的方法,与此有关的问题在互联网上,但未能删除警告 我在componentDidMount中设置了isMounted的标志并将其设置为true,但没有帮助。。我还尝试在componentWillUnMount中清除间隔和超时,但仍然失败 login = () => { var mythis = this; var name = this
login = () => {
var mythis = this;
var name = this.state.name;
var password = this.state.password
this.setState({isLoading:true,show:true});
this.interval = setInterval(function(){
mythis.setState({show:true})
},300);
$.ajax({
url : 'http://localhost:4000/login',
type : "POST",
data : {username:name,password:password},
success : function(data){
mythis.firstTimeout = setTimeout(function(){
clearInterval(mythis.interval);
if(data == true){
mythis.setState({show:false});
mythis.secondTimeout=setTimeout(function(){
mythis.setState({isLoading:false});
},200)
}
else {
mythis.setState({show:false});
mythis.secondTimeout=setTimeout(function(){
mythis.setState({isLoading:false})
},200)
}
},2000)
}.bind(this)
})
}
componentWillUnMount(){
//this._isMounted = false;
clearInterval(this.interval);
clearTimeout(this.firstTimeout);
clearTimeout(this.secondTimeout);
}
render(){
return (
<div>
{this.state.isLoading ? (
<div>
<Loading
show={this.state.show}
color="red"
showSpinner={false}
/>
</div>
):''}
}
</div>
)
login=()=>{
var mythis=此;
var name=this.state.name;
var password=this.state.password
this.setState({isLoading:true,show:true});
this.interval=setInterval(函数(){
mythis.setState({show:true})
},300);
$.ajax({
网址:'http://localhost:4000/login',
类型:“POST”,
数据:{用户名:名称,密码:密码},
成功:功能(数据){
mythis.firstTimeout=setTimeout(函数(){
clearInterval(mythis.interval);
如果(数据==true){
setState({show:false});
mythis.secondTimeout=setTimeout(函数(){
mythis.setState({isLoading:false});
},200)
}
否则{
setState({show:false});
mythis.secondTimeout=setTimeout(函数(){
mythis.setState({isLoading:false})
},200)
}
},2000)
}.绑定(此)
})
}
组件将卸载(){
//这个。_isMounted=false;
clearInterval(这个.interval);
clearTimeout(此为.firstTimeout);
clearTimeout(此.secondTimeout);
}
render(){
返回(
{this.state.isLoading(
):''}
}
)
如果这是您正在构建的react应用程序,则在componentDidMount生命周期方法中调用ajax请求。
首先设置您的私有变量
\u isMounted=false
然后在componentDidMount中:
componentDidMount() {
this._isMounted = true;
.... your ajax request here
}
在ajax成功函数中:
if (this._isMounted) {
this.setState({ isLoading: false });
}
然后在componentWillUnmount生命周期方法中:
componentWillUnmount(){
this._isMounted= false
}
功能组件呢?在功能组件中,您实现了useEffect挂钩。在这个钩子中运行一个清理函数,设置
\u isMounted=false
,从而模拟组件将卸载