-
Notifications
You must be signed in to change notification settings - Fork 50.9k
[compiler] More flexible/helpful lazy ref initialization #34449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
...er/src/__tests__/fixtures/compiler/allow-ref-initialization-undefined.expect.md
This file contains hidden or 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,42 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| //@flow | ||
| import {useRef} from 'react'; | ||
|
|
||
| component C() { | ||
| const r = useRef(null); | ||
| if (r.current == undefined) { | ||
| r.current = 1; | ||
| } | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { useRef } from "react"; | ||
|
|
||
| function C() { | ||
| const r = useRef(null); | ||
| if (r.current == undefined) { | ||
| r.current = 1; | ||
| } | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) | ||
14 changes: 14 additions & 0 deletions
14
...ugin-react-compiler/src/__tests__/fixtures/compiler/allow-ref-initialization-undefined.js
This file contains hidden or 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,14 @@ | ||
| //@flow | ||
| import {useRef} from 'react'; | ||
|
|
||
| component C() { | ||
| const r = useRef(null); | ||
| if (r.current == undefined) { | ||
| r.current = 1; | ||
| } | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; |
78 changes: 78 additions & 0 deletions
78
...src/__tests__/fixtures/compiler/error.invalid-ref-access-render-unary.expect.md
This file contains hidden or 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,78 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| //@flow | ||
| import {useRef} from 'react'; | ||
|
|
||
| component C() { | ||
| const r = useRef(null); | ||
| const current = !r.current; | ||
| return <div>{current}</div>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 4 errors: | ||
|
|
||
| Error: Cannot access refs during render | ||
|
|
||
| React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). | ||
|
|
||
| 4 | component C() { | ||
| 5 | const r = useRef(null); | ||
| > 6 | const current = !r.current; | ||
| | ^^^^^^^^^ Cannot access ref value during render | ||
| 7 | return <div>{current}</div>; | ||
| 8 | } | ||
| 9 | | ||
|
|
||
| To initialize a ref only once, check that the ref is null with the pattern `if (ref.current == null) { ref.current = ... }` | ||
|
|
||
| Error: Cannot access refs during render | ||
|
|
||
| React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). | ||
|
|
||
| 4 | component C() { | ||
| 5 | const r = useRef(null); | ||
| > 6 | const current = !r.current; | ||
| | ^^^^^^^^^^ Cannot access ref value during render | ||
| 7 | return <div>{current}</div>; | ||
| 8 | } | ||
| 9 | | ||
|
|
||
| Error: Cannot access refs during render | ||
|
|
||
| React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). | ||
|
|
||
| 5 | const r = useRef(null); | ||
| 6 | const current = !r.current; | ||
| > 7 | return <div>{current}</div>; | ||
| | ^^^^^^^ Cannot access ref value during render | ||
| 8 | } | ||
| 9 | | ||
| 10 | export const FIXTURE_ENTRYPOINT = { | ||
|
|
||
| Error: Cannot access refs during render | ||
|
|
||
| React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). | ||
|
|
||
| 5 | const r = useRef(null); | ||
| 6 | const current = !r.current; | ||
| > 7 | return <div>{current}</div>; | ||
| | ^^^^^^^ Cannot access ref value during render | ||
| 8 | } | ||
| 9 | | ||
| 10 | export const FIXTURE_ENTRYPOINT = { | ||
| ``` | ||
|
|
||
|
|
13 changes: 13 additions & 0 deletions
13
...n-react-compiler/src/__tests__/fixtures/compiler/error.invalid-ref-access-render-unary.js
This file contains hidden or 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,13 @@ | ||
| //@flow | ||
| import {useRef} from 'react'; | ||
|
|
||
| component C() { | ||
| const r = useRef(null); | ||
| const current = !r.current; | ||
| return <div>{current}</div>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; |
43 changes: 43 additions & 0 deletions
43
..._tests__/fixtures/compiler/error.invalid-ref-initialization-unary-not.expect.md
This file contains hidden or 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,43 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| //@flow | ||
| import {useRef} from 'react'; | ||
|
|
||
| component C() { | ||
| const r = useRef(null); | ||
| if (!r.current) { | ||
| r.current = 1; | ||
| } | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: C, | ||
| params: [{}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Error: Cannot access refs during render | ||
|
|
||
| React refs are values that are not needed for rendering. Refs should only be accessed outside of render, such as in event handlers or effects. Accessing a ref value (the `current` property) during render can cause your component not to update as expected (https://react.dev/reference/react/useRef). | ||
|
|
||
| 4 | component C() { | ||
| 5 | const r = useRef(null); | ||
| > 6 | if (!r.current) { | ||
| | ^^^^^^^^^ Cannot access ref value during render | ||
| 7 | r.current = 1; | ||
| 8 | } | ||
| 9 | } | ||
|
|
||
| To initialize a ref only once, check that the ref is null with the pattern `if (ref.current == null) { ref.current = ... }` | ||
| ``` | ||
|
|
||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it correct to compare with an
undefinedvalue when useRef was initialized withnull?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but it’s valid if you initialized w undefined or no initializer argument. We could try to be more precise and validate that you’re comparing exactly against the initializer value, but this feels close enough.