-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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-source-id: 76cd07c5dfeea2a4aafe141912663b097308fd73 Pull Request resolved: #29834
- Loading branch information
1 parent
bf1bb2e
commit c50d593
Showing
8 changed files
with
107 additions
and
1 deletion.
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
25 changes: 25 additions & 0 deletions
25
..._/fixtures/compiler/error.invalid-read-ref-prop-in-render-destructure.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,25 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component({ ref }) { | ||
const value = ref.current; | ||
return <div>{value}</div>; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | function Component({ ref }) { | ||
3 | const value = ref.current; | ||
> 4 | return <div>{value}</div>; | ||
| ^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at read $17:TObject<BuiltInRefValue> (4:4) | ||
5 | } | ||
6 | | ||
``` | ||
5 changes: 5 additions & 0 deletions
5
...iler/src/__tests__/fixtures/compiler/error.invalid-read-ref-prop-in-render-destructure.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,5 @@ | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component({ ref }) { | ||
const value = ref.current; | ||
return <div>{value}</div>; | ||
} |
25 changes: 25 additions & 0 deletions
25
...fixtures/compiler/error.invalid-read-ref-prop-in-render-property-load.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,25 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component(props) { | ||
const value = props.ref.current; | ||
return <div>{value}</div>; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
2 | function Component(props) { | ||
3 | const value = props.ref.current; | ||
> 4 | return <div>{value}</div>; | ||
| ^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at read $15:TObject<BuiltInRefValue> (4:4) | ||
5 | } | ||
6 | | ||
``` | ||
5 changes: 5 additions & 0 deletions
5
...er/src/__tests__/fixtures/compiler/error.invalid-read-ref-prop-in-render-property-load.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,5 @@ | ||
// @validateRefAccessDuringRender @compilationMode(infer) | ||
function Component(props) { | ||
const value = props.ref.current; | ||
return <div>{value}</div>; | ||
} |
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>; | ||
} |