2021-06-08 14:23:48 +02:00

53 lines
1.1 KiB
Plaintext

``++isMounted()++`` is generally used before calling ``++setState++`` to avoid changing the state on an already unmounted component. A user should never be able to do an action on an unmount component unless the component is having a bug in its unmount phase.
This rule raises an issue on each use of ``++isMounted()++``.
``++isMounted()++`` is not longer available in ES6. Use of ``++isMounted()++`` must be avoided to ease the upgrade to ES6.
== Noncompliant Code Example
----
if (this.isMounted()) { // Noncompliant
this.setState({...});
}
----
== Compliant Solution
----
class MyComponent extends React.Component {
_isMounted = false;
componentDidMount() {
_isMounted = true;
...
if (_isMounted) {
this.setState({...});
}
}
render() {
...
}
componentWillUnmount() {
_isMounted = false;
}
}
----
== See
* https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html[React.js blog] - isMounted is an Antipattern
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]