Why this matters
The isMounted method is deprecated and hides warnings about improper state updates. Instead, use cleanup functions or other lifecycle methods to handle unmounted components safely.
Ensure that isMounted is not used in React components. This method is deprecated and can cause unreliable state updates. Suggest using cleanup functions or useRef instead.
The isMounted method is deprecated and hides warnings about improper state updates. Instead, use cleanup functions or other lifecycle methods to handle unmounted components safely.
Side-by-side examples engineers can pattern-match during review.
class MyComponent extends React.Component {
componentDidMount() {
mydatastore.subscribe(this);
}
dataHandler() {
if (this.isMounted()) { // Noncompliant: isMounted() hides the error
//...
}
}
render() {
//... calls dataHandler()
}
};class MyComponent extends React.Component {
componentDidMount() {
mydatastore.subscribe(this);
}
dataHandler() {
//...
}
render() {
//...
}
componentWillUnmount() {
mydatastore.unsubscribe(this);
}
}class MyComponent extends React.Component {
componentDidMount() {
mydatastore.subscribe(this);
}
dataHandler() {
if (this.isMounted()) { // Noncompliant: isMounted() hides the error
//...
}
}
render() {
//... calls dataHandler()
}
};class MyComponent extends React.Component {
componentDidMount() {
mydatastore.subscribe(this);
}
dataHandler() {
//...
}
render() {
//...
}
componentWillUnmount() {
mydatastore.unsubscribe(this);
}
}From the same buckets as this rule.