-
Notifications
You must be signed in to change notification settings - Fork 47.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Fixture to show ref-in-render enforcement issue with useCa…
…llback Test Plan: Documents that useCallback calls interfere with it being ok for refs to escape as part of functions into jsx ghstack-source-id: a5df427981ca32406fb2325e583b64bbe26b1cdd Pull Request resolved: #30714
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...ct-compiler/src/__tests__/fixtures/compiler/error.return-ref-callback.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,37 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees | ||
|
||
component Foo() { | ||
const ref = useRef(); | ||
|
||
const s = () => { | ||
return ref.current; | ||
}; | ||
|
||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
8 | }; | ||
9 | | ||
> 10 | return s; | ||
| ^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at freeze $25:TObject<BuiltInFunction> (10:10) | ||
11 | } | ||
12 | | ||
13 | export const FIXTURE_ENTRYPOINT = { | ||
``` | ||
16 changes: 16 additions & 0 deletions
16
.../babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.return-ref-callback.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,16 @@ | ||
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees | ||
|
||
component Foo() { | ||
const ref = useRef(); | ||
|
||
const s = () => { | ||
return ref.current; | ||
}; | ||
|
||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; |
41 changes: 41 additions & 0 deletions
41
...piler/src/__tests__/fixtures/compiler/error.useCallback-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,41 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees | ||
|
||
component Foo() { | ||
const ref = useRef(); | ||
|
||
const s = useCallback(() => { | ||
return ref.current; | ||
}); | ||
|
||
return <a r={s} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
4 | const ref = useRef(); | ||
5 | | ||
> 6 | const s = useCallback(() => { | ||
| ^^^^^^^ | ||
> 7 | return ref.current; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 8 | }); | ||
| ^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef). Cannot access ref value at read $27:TObject<BuiltInFunction> (6:8) | ||
9 | | ||
10 | return <a r={s} />; | ||
11 | } | ||
``` | ||
16 changes: 16 additions & 0 deletions
16
...-plugin-react-compiler/src/__tests__/fixtures/compiler/error.useCallback-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,16 @@ | ||
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees | ||
|
||
component Foo() { | ||
const ref = useRef(); | ||
|
||
const s = useCallback(() => { | ||
return ref.current; | ||
}); | ||
|
||
return <a r={s} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; |