-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update on "[compiler] Type
ref
prop as a ref"
Adds a shape type for component props, which has one defined property: "ref". This means that if the ref property exists, we can type usage of `props.ref` (or via destructuring) the same as the result of `useRef()` and infer downstream usage similarly. [ghstack-poisoned]
- Loading branch information
1 parent
dae5020
commit 9f141fa
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...rc/__tests__/fixtures/compiler/error.invalid-write-ref-prop-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,27 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component(props) { | ||
const ref = props.ref; | ||
ref.current = true; | ||
return <div>{value}</div>; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | function Component(props) { | ||
3 | const ref = props.ref; | ||
> 4 | ref.current = true; | ||
| ^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (4:4) | ||
5 | return <div>{value}</div>; | ||
6 | } | ||
7 | | ||
``` | ||
6 changes: 6 additions & 0 deletions
6
...-react-compiler/src/__tests__/fixtures/compiler/error.invalid-write-ref-prop-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,6 @@ | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component(props) { | ||
const ref = props.ref; | ||
ref.current = true; | ||
return <div>{value}</div>; | ||
} |