Pattern: Use of isMounted
method
Issue: -
The isMounted
method is considered an anti-pattern and is not available in ES6 class components. It has been deprecated as it often indicates underlying issues in component lifecycle management.
Example of incorrect code:
class Hello extends React.Component {
someMethod() {
if (!this.isMounted()) {
return;
}
}
render() {
return <div onClick={this.someMethod.bind(this)}>Hello</div>;
}
}
Example of correct code:
class Hello extends React.Component {
_isMounted = false;
componentDidMount() {
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
}
someMethod() {
if (!this._isMounted) {
return;
}
}
}