-
Notifications
You must be signed in to change notification settings - Fork 47.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: ValidateNoRefInRender detects writes of refs
Improves ValidateNoRefAccessInRender, detecting modifications of refs during render. Fixes #29161 ghstack-source-id: 99078b3cea5b2d9019dbf77ede9c2e4cd9fbfd27 Pull Request resolved: #29170
- Loading branch information
1 parent
afb2c39
commit e37f156
Showing
11 changed files
with
132 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...compiler/error.invalid-set-and-read-ref-nested-property-during-render.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateRefAccessDuringRender | ||
function Component(props) { | ||
const ref = useRef({ inner: null }); | ||
ref.current.inner = props.value; | ||
return ref.current.inner; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | function Component(props) { | ||
3 | const ref = useRef({ inner: null }); | ||
> 4 | ref.current.inner = props.value; | ||
| ^^^^^^^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (4:4) | ||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at freeze $30:TObject<BuiltInRefValue> (5:5) | ||
5 | return ref.current.inner; | ||
6 | } | ||
7 | | ||
``` | ||
6 changes: 6 additions & 0 deletions
6
...tests__/fixtures/compiler/error.invalid-set-and-read-ref-nested-property-during-render.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @validateRefAccessDuringRender | ||
function Component(props) { | ||
const ref = useRef({ inner: null }); | ||
ref.current.inner = props.value; | ||
return ref.current.inner; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...s__/fixtures/compiler/error.invalid-write-but-dont-read-ref-in-render.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateRefAccessDuringRender | ||
function useHook({ value }) { | ||
const ref = useRef(null); | ||
// Writing to a ref in render is against the rules: | ||
ref.current = value; | ||
// returning a ref is allowed, so this alone doesn't trigger an error: | ||
return ref; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
3 | const ref = useRef(null); | ||
4 | // Writing to a ref in render is against the rules: | ||
> 5 | ref.current = value; | ||
| ^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (5:5) | ||
6 | // returning a ref is allowed, so this alone doesn't trigger an error: | ||
7 | return ref; | ||
8 | } | ||
``` | ||
8 changes: 8 additions & 0 deletions
8
...mpiler/src/__tests__/fixtures/compiler/error.invalid-write-but-dont-read-ref-in-render.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @validateRefAccessDuringRender | ||
function useHook({ value }) { | ||
const ref = useRef(null); | ||
// Writing to a ref in render is against the rules: | ||
ref.current = value; | ||
// returning a ref is allowed, so this alone doesn't trigger an error: | ||
return ref; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters