Skip to content

Files

Latest commit

 

History

History
44 lines (35 loc) · 804 Bytes

no-is-mounted.md

File metadata and controls

44 lines (35 loc) · 804 Bytes

Pattern: Use of isMounted method

Issue: -

Description

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.

Examples

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;
    }
  }
}