-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
[compiler] Allow all hooks to take callbacks which access refs, but ban hooks from taking direct ref value arguments #30917
Merged
Merged
Changes from 1 commit
Commits
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 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
34 changes: 34 additions & 0 deletions
34
...n-react-compiler/src/__tests__/fixtures/compiler/error.hook-ref-value.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,34 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const ref = useRef(); | ||
useEffect(() => {}, [ref.current]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
3 | function Component(props) { | ||
4 | const ref = useRef(); | ||
> 5 | useEffect(() => {}, [ref.current]); | ||
| ^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (5:5) | ||
|
||
InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (5:5) | ||
6 | } | ||
7 | | ||
8 | export const FIXTURE_ENTRYPOINT = { | ||
``` | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
...kages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/error.hook-ref-value.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,11 @@ | ||
import {useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const ref = useRef(); | ||
useEffect(() => {}, [ref.current]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [], | ||
}; |
54 changes: 54 additions & 0 deletions
54
...ugin-react-compiler/src/__tests__/fixtures/compiler/hook-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,54 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const ref = useRef(); | ||
useFoo(() => { | ||
ref.current = 42; | ||
}); | ||
} | ||
|
||
function useFoo(x) {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useEffect } from "react"; | ||
|
||
function Component(props) { | ||
const $ = _c(1); | ||
const ref = useRef(); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = () => { | ||
ref.current = 42; | ||
}; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
useFoo(t0); | ||
} | ||
|
||
function useFoo(x) {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: exception) useRef is not defined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops |
15 changes: 15 additions & 0 deletions
15
...packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/hook-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,15 @@ | ||
import {useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const ref = useRef(); | ||
useFoo(() => { | ||
ref.current = 42; | ||
}); | ||
} | ||
|
||
function useFoo(x) {} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [], | ||
}; |
66 changes: 66 additions & 0 deletions
66
...mpiler/src/__tests__/fixtures/compiler/useImperativeHandle-ref-mutate.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,66 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow | ||
|
||
import {useImperativeHandle, useRef} from 'react'; | ||
|
||
component Component(prop: number) { | ||
const ref1 = useRef(null); | ||
const ref2 = useRef(1); | ||
useImperativeHandle(ref1, () => { | ||
const precomputed = prop + ref2.current; | ||
return { | ||
foo: () => prop + ref2.current + precomputed, | ||
}; | ||
}, [prop]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{prop: 1}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
|
||
import { useImperativeHandle, useRef } from "react"; | ||
|
||
function Component(t0) { | ||
const $ = _c(3); | ||
const { prop } = t0; | ||
const ref1 = useRef(null); | ||
const ref2 = useRef(1); | ||
let t1; | ||
let t2; | ||
if ($[0] !== prop) { | ||
t1 = () => { | ||
const precomputed = prop + ref2.current; | ||
return { foo: () => prop + ref2.current + precomputed }; | ||
}; | ||
|
||
t2 = [prop]; | ||
$[0] = prop; | ||
$[1] = t1; | ||
$[2] = t2; | ||
} else { | ||
t1 = $[1]; | ||
t2 = $[2]; | ||
} | ||
useImperativeHandle(ref1, t1, t2); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ prop: 1 }], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) |
19 changes: 19 additions & 0 deletions
19
...l-plugin-react-compiler/src/__tests__/fixtures/compiler/useImperativeHandle-ref-mutate.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,19 @@ | ||
// @flow | ||
|
||
import {useImperativeHandle, useRef} from 'react'; | ||
|
||
component Component(prop: number) { | ||
const ref1 = useRef(null); | ||
const ref2 = useRef(1); | ||
useImperativeHandle(ref1, () => { | ||
const precomputed = prop + ref2.current; | ||
return { | ||
foo: () => prop + ref2.current + precomputed, | ||
}; | ||
}, [prop]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{prop: 1}], | ||
}; |
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.
This is left over from a previous version of this work, but seems reasonable to add anyways