Warning: Can’t perform a React state update on an unmounted component. This is a no-op, but it indic
In the development of react, we may often encounter this warning:
Can't perform a React state update on an unmounted component.
This is a no-op, but it indicates a memory leak in your application.
We cannot set state after the component is destroyed to prevent memory leakage
As for the above errors when switching routes in react, the actual reason is that after the component is mounted, you perform asynchronous operations, such as Ajax requests or setting timers, and you perform setstate operation in callback. When you switch routes, the component has been unmounted. At this time, the callback is still executing in the asynchronous operation, so the setstate does not get a value.
There are two solutions:
1. Clear all operations when uninstalling (e.g. abort your Ajax request or clear timer)
componentDidMount = () => {
//1.ajax request
$.ajax('Your request',{})
.then(res => {
this.setState({
aa:true
})
})
.catch(err => {})
//2.timer
timer = setTimeout(() => {
//dosomething
},1000)
}
componentWillUnMount = () => {
//1.ajax request
$.ajax.abort()
//2.timer
clearTimeout(timer)
}
2. Set a flag and reset it when unmount
componentDidMount = () => {
this._isMounted = true;
$.ajax('Your Request',{})
.then(res => {
if(this._isMounted){
this.setState({
aa:true
})
}
})
.catch(err => {})
}
componentWillUnMount = () => {
this._isMounted = false;
}
3. The simplest way (oil of gold)
componentDidMount = () => {
$.ajax('Your Request',{})
.then(res => {
this.setState({
data: datas,
});
})
.catch(error => {
});
}
componentWillUnmount = () => {
this.setState = (state,callback)=>{
return;
};
}
Read More:
- React Hook “useState“ is called in function “xxx“ which is neither a React function component or
- [Solved] swiper Error: The requested module ‘react’ is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export
- NPM install -g create-react-app [How to Solve]
- [Solved] react18 swiper Error: Cannot read properties of undefined (reading ‘wrapperClass’)
- PgSQL associated tables perform update to avoid using in query SQL
- Taro Use React Hooks Error: TypeError: Object(…) is not a function
- Attempted import error: ‘mobx-react‘ does not contain a default export (imported as ‘observer‘).
- React-native Error: Make sure you have the Android development environment set up [Solved]
- [Solved] React Native Red screen Error: Unable to load script from assets
- [Solved] React import the Path of Image error: cannot find mouse ‘.‘
- React ant design pro Error reported in the background after removing internationalization
- [Solved] npm Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- [Solved] Rabbitmq Server Error: unable to perform an operation on node ‘rabbit@nscczzms‘. P
- [Solved] redis Error: Can‘t save in background fork Cannot allocate memory
- How to Solve Angular Error: error NG8002: Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘input‘.
- [Solved] Error: unable to perform an operation on node ‘rabbit@DESKTOP-xxx‘. Please see diagnostics informa
- [Solved] kubeflow pipeline visualization component Error: WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager…
- Sqoop Error: Can‘t parse input data: ‘\N‘ [How to Solve]
- [Solved] webpack.config.js configuration encountered Error: Cannot find module’@babel/core’&&Cannot find module’@babel/plugin-transform-react-jsx’
- [Solved] Yolox Run Error: can‘t find starting number