Commit 31940cd
committed
[compiler] Option to treat "set-" prefixed callees as setState functions (facebook#34505)
Calling setState functions during render can lead to extraneous renders
or even infinite loops. We also have runtime detection for loops, but
static detection is obviously even better.
This PR adds an option to infer identifers as setState functions if both
the following conditions are met:
- The identifier is named starting with "set"
- The identifier is used as the callee of a call expression
By inferring values as SetState type, this allows our existing
ValidateNoSetStateInRender rule to flag calls during render, disallowing
examples like the following:
```js
function Component({setParentState}) {
setParentState(...);
^^^^^^^^^^^^^^ Error: Cannot call setState in render
}
```
DiffTrain build for [7899729](facebook@7899729)1 parent c706b43 commit 31940cd
File tree
35 files changed
+95
-87
lines changed- compiled
- eslint-plugin-react-hooks
- facebook-www
35 files changed
+95
-87
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32155 | 32155 | | |
32156 | 32156 | | |
32157 | 32157 | | |
| 32158 | + | |
32158 | 32159 | | |
32159 | 32160 | | |
32160 | 32161 | | |
| |||
48008 | 48009 | | |
48009 | 48010 | | |
48010 | 48011 | | |
| 48012 | + | |
| 48013 | + | |
| 48014 | + | |
| 48015 | + | |
| 48016 | + | |
| 48017 | + | |
| 48018 | + | |
48011 | 48019 | | |
48012 | 48020 | | |
48013 | | - | |
| 48021 | + | |
48014 | 48022 | | |
48015 | 48023 | | |
48016 | 48024 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
1422 | | - | |
| 1422 | + | |
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
1422 | | - | |
| 1422 | + | |
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | | - | |
| 605 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | | - | |
| 605 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
609 | | - | |
| 609 | + | |
610 | 610 | | |
611 | 611 | | |
612 | 612 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
606 | 606 | | |
607 | 607 | | |
608 | 608 | | |
609 | | - | |
| 609 | + | |
610 | 610 | | |
611 | 611 | | |
612 | 612 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19765 | 19765 | | |
19766 | 19766 | | |
19767 | 19767 | | |
19768 | | - | |
| 19768 | + | |
19769 | 19769 | | |
19770 | 19770 | | |
19771 | | - | |
| 19771 | + | |
19772 | 19772 | | |
19773 | 19773 | | |
19774 | 19774 | | |
| |||
19802 | 19802 | | |
19803 | 19803 | | |
19804 | 19804 | | |
19805 | | - | |
| 19805 | + | |
19806 | 19806 | | |
19807 | 19807 | | |
19808 | 19808 | | |
| |||
0 commit comments