Skip to content

Commit 42a8093

Browse files
committed
[New] no-did-mount-set-state, no-did-update-set-state: no-op with react >= 16.3
Fixes #1754
1 parent 0a0a2e1 commit 42a8093

File tree

4 files changed

+451
-409
lines changed

4 files changed

+451
-409
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1010
* [`jsx-no-target-blank`]: Improve fixer with option `allowReferrer` ([#3167][] @apepper)
1111
* [`jsx-curly-brace-presence`]: add "propElementValues" config option ([#3191][] @ljharb)
1212
* add [`iframe-missing-sandbox`] rule ([#2753][] @tosmolka @ljharb)
13+
* [`no-did-mount-set-state`], [`no-did-update-set-state`]: no-op with react >= 16.3 ([#1754][] @ljharb)
1314

1415
### Fixed
1516
* [`prop-types`], `propTypes`: add support for exported type inference ([#3163][] @vedadeepta)
@@ -42,6 +43,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
4243
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
4344
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
4445
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
46+
[#1754]: https://github.com/yannickcr/eslint-plugin-react/issues/1754
4547
[#1046]: https://github.com/yannickcr/eslint-plugin-react/issues/1046
4648
[#620]: https://github.com/yannickcr/eslint-plugin-react/pull/620
4749
[#519]: https://github.com/yannickcr/eslint-plugin-react/issues/519

lib/util/makeNoMethodSetStateRule.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const docsUrl = require('./docsUrl');
99
const report = require('./report');
10+
const testReactVersion = require('./version').testReactVersion;
1011

1112
// ------------------------------------------------------------------------------
1213
// Rule Definition
@@ -29,7 +30,18 @@ const messages = {
2930
noSetState: 'Do not use setState in {{name}}',
3031
};
3132

32-
function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
33+
const methodNoopsAsOf = {
34+
componentDidMount: '>= 16.3.0',
35+
componentDidUpdate: '>= 16.3.0',
36+
};
37+
38+
function shouldBeNoop(context, methodName) {
39+
return methodName in methodNoopsAsOf
40+
&& testReactVersion(context, methodNoopsAsOf[methodName])
41+
&& !testReactVersion(context, '999.999.999'); // for when the version is not specified
42+
}
43+
44+
module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
3345
return {
3446
meta: {
3547
docs: {
@@ -67,6 +79,10 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
6779

6880
return {
6981
CallExpression(node) {
82+
if (shouldBeNoop(context, methodName)) {
83+
return;
84+
}
85+
7086
const callee = node.callee;
7187
if (
7288
callee.type !== 'MemberExpression'
@@ -100,6 +116,4 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
100116
};
101117
},
102118
};
103-
}
104-
105-
module.exports = makeNoMethodSetStateRule;
119+
};

0 commit comments

Comments
 (0)