Commit 27fa34a
committed
[compiler] Extend setState in effect validation to useEffectEvent (facebook#35214)
ValidateNoSetStateInEffects already supports transitive setter
functions. This PR marks any synchonous state setter useEffectEvent
function so we can validate that uEE isn't being used only as
misdirection to avoid the validation within an effect body.
The error points to the call of the effect event.
Example:
```js
export default function MyApp() {
const [count, setCount] = useState(0)
const effectEvent = useEffectEvent(() => {
setCount(10)
})
useEffect(() => {
effectEvent()
}, [])
return <div>{count}</div>;
```
```
Found 1 error:
Error: Calling setState synchronously within an effect can trigger cascading renders
Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:
* Update external systems with the latest state from React.
* Subscribe for updates from some external system, calling setState in a callback function when external state changes.
Calling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect).
5 | })
6 | useEffect(() => {
> 7 | effectEvent()
| ^^^^^^^^^^^ Avoid calling setState() directly within an effect
8 | }, [])
9 | return <div>{count}</div>;
10 | }
```
DiffTrain build for [09f0569](facebook@09f0569)1 parent 352146f commit 27fa34a
File tree
23 files changed
+100
-76
lines changed- compiled-rn
- facebook-fbsource/xplat/js
- RKJSModules/vendor/react
- react-dom/cjs
- react-test-renderer/cjs
- react/cjs
- react-native-github/Libraries/Renderer
- implementations
- tools/eslint-plugin-react-hooks
- cjs
23 files changed
+100
-76
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
410 | 410 | | |
411 | 411 | | |
412 | 412 | | |
413 | | - | |
| 413 | + | |
414 | 414 | | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
212 | | - | |
| 212 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
212 | | - | |
| 212 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
30244 | 30244 | | |
30245 | 30245 | | |
30246 | 30246 | | |
30247 | | - | |
| 30247 | + | |
30248 | 30248 | | |
30249 | 30249 | | |
30250 | 30250 | | |
30251 | | - | |
| 30251 | + | |
30252 | 30252 | | |
30253 | 30253 | | |
30254 | 30254 | | |
| |||
30285 | 30285 | | |
30286 | 30286 | | |
30287 | 30287 | | |
30288 | | - | |
| 30288 | + | |
30289 | 30289 | | |
30290 | 30290 | | |
30291 | | - | |
| 30291 | + | |
30292 | 30292 | | |
30293 | 30293 | | |
30294 | 30294 | | |
| |||
30438 | 30438 | | |
30439 | 30439 | | |
30440 | 30440 | | |
30441 | | - | |
| 30441 | + | |
30442 | 30442 | | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17707 | 17707 | | |
17708 | 17708 | | |
17709 | 17709 | | |
17710 | | - | |
| 17710 | + | |
17711 | 17711 | | |
17712 | 17712 | | |
17713 | 17713 | | |
17714 | 17714 | | |
17715 | 17715 | | |
17716 | 17716 | | |
17717 | | - | |
| 17717 | + | |
17718 | 17718 | | |
17719 | 17719 | | |
17720 | 17720 | | |
| |||
17736 | 17736 | | |
17737 | 17737 | | |
17738 | 17738 | | |
17739 | | - | |
| 17739 | + | |
17740 | 17740 | | |
17741 | 17741 | | |
17742 | | - | |
| 17742 | + | |
17743 | 17743 | | |
17744 | 17744 | | |
17745 | 17745 | | |
| |||
17846 | 17846 | | |
17847 | 17847 | | |
17848 | 17848 | | |
17849 | | - | |
| 17849 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
19833 | 19833 | | |
19834 | 19834 | | |
19835 | 19835 | | |
19836 | | - | |
| 19836 | + | |
19837 | 19837 | | |
19838 | 19838 | | |
19839 | 19839 | | |
19840 | 19840 | | |
19841 | 19841 | | |
19842 | 19842 | | |
19843 | | - | |
| 19843 | + | |
19844 | 19844 | | |
19845 | 19845 | | |
19846 | 19846 | | |
| |||
19862 | 19862 | | |
19863 | 19863 | | |
19864 | 19864 | | |
19865 | | - | |
| 19865 | + | |
19866 | 19866 | | |
19867 | 19867 | | |
19868 | | - | |
| 19868 | + | |
19869 | 19869 | | |
19870 | 19870 | | |
19871 | 19871 | | |
| |||
19988 | 19988 | | |
19989 | 19989 | | |
19990 | 19990 | | |
19991 | | - | |
| 19991 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
30306 | 30306 | | |
30307 | 30307 | | |
30308 | 30308 | | |
30309 | | - | |
| 30309 | + | |
30310 | 30310 | | |
30311 | 30311 | | |
30312 | 30312 | | |
30313 | | - | |
| 30313 | + | |
30314 | 30314 | | |
30315 | 30315 | | |
30316 | 30316 | | |
| |||
30347 | 30347 | | |
30348 | 30348 | | |
30349 | 30349 | | |
30350 | | - | |
| 30350 | + | |
30351 | 30351 | | |
30352 | 30352 | | |
30353 | | - | |
| 30353 | + | |
30354 | 30354 | | |
30355 | 30355 | | |
30356 | 30356 | | |
| |||
30816 | 30816 | | |
30817 | 30817 | | |
30818 | 30818 | | |
30819 | | - | |
| 30819 | + | |
30820 | 30820 | | |
30821 | 30821 | | |
30822 | 30822 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17724 | 17724 | | |
17725 | 17725 | | |
17726 | 17726 | | |
17727 | | - | |
| 17727 | + | |
17728 | 17728 | | |
17729 | 17729 | | |
17730 | 17730 | | |
17731 | 17731 | | |
17732 | 17732 | | |
17733 | 17733 | | |
17734 | | - | |
| 17734 | + | |
17735 | 17735 | | |
17736 | 17736 | | |
17737 | 17737 | | |
| |||
17753 | 17753 | | |
17754 | 17754 | | |
17755 | 17755 | | |
17756 | | - | |
| 17756 | + | |
17757 | 17757 | | |
17758 | 17758 | | |
17759 | | - | |
| 17759 | + | |
17760 | 17760 | | |
17761 | 17761 | | |
17762 | 17762 | | |
| |||
18016 | 18016 | | |
18017 | 18017 | | |
18018 | 18018 | | |
18019 | | - | |
| 18019 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
19854 | 19854 | | |
19855 | 19855 | | |
19856 | 19856 | | |
19857 | | - | |
| 19857 | + | |
19858 | 19858 | | |
19859 | 19859 | | |
19860 | 19860 | | |
19861 | 19861 | | |
19862 | 19862 | | |
19863 | 19863 | | |
19864 | | - | |
| 19864 | + | |
19865 | 19865 | | |
19866 | 19866 | | |
19867 | 19867 | | |
| |||
19883 | 19883 | | |
19884 | 19884 | | |
19885 | 19885 | | |
19886 | | - | |
| 19886 | + | |
19887 | 19887 | | |
19888 | 19888 | | |
19889 | | - | |
| 19889 | + | |
19890 | 19890 | | |
19891 | 19891 | | |
19892 | 19892 | | |
| |||
20162 | 20162 | | |
20163 | 20163 | | |
20164 | 20164 | | |
20165 | | - | |
| 20165 | + | |
20166 | 20166 | | |
20167 | 20167 | | |
20168 | 20168 | | |
| |||
0 commit comments