This error is obvious, because when the component is destroyed, people use the method of setstate, but there is no such method as weak in OC/swift in RN, here is a method to solve this error:
import React,{PureComponent} from 'react';
function inject_unount(Target){
// Retrofit componentWillUnmount, record when destroyed (when used by subclasses, super is required)
var next = Target.prototype.componentWillUnmount;
Target.prototype.componentWillUnmount = function() {
if (next) {
next.call(this, ...arguments);
}
this.unmount = true;
};
// Modifications to setState, setState to see if it is currently destroyed
let setState = Target.prototype.setState;
Target.prototype.setState = function() {
// if (this.unmount) {
// return;
// }
setState.call(this, ...arguments);
};
}
@inject_unount
export default class BaseComponent extends PureComponent {}
Other components inherit from basecomponent
import BaseComponent from 'xxx';
export default class HZJDemo extends BaseComponent {
//If you override this method, remember to super
componentWillUnmount = () => {
super.componentWillUnmount();
};
}
Similar Posts:
- Can’t call setState (or forceUpdate) on an unmounted component [How to Solve]
- How to deal with the error “cannot read property ‘set state’ of undefined” in react
- How to Use Array.prototype.slice.call(arguments)
- React Common Component Issues: Can’t perform a React state update on an unmounted component [How to Solve]
- [Solved] Vue router common problems (push error, push duplicate route refresh)
- DOM style setting methods in the four components of react
- [Solved] Vue route Duplicate Error: error avoid redundant navigation to current location: “/home”
- Uncaught Error: _registerComponent(…): Target container is not a DOM element
- Python Typeerror: super() takes at least 1 argument (0 given) error in Python