-
Notifications
You must be signed in to change notification settings - Fork 47.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Allow all hooks to take callbacks which access refs, but b…
…an hooks from taking direct ref value arguments Summary: This brings the behavior of ref mutation within hook callbacks into alignment with the behavior of global mutations--that is, we allow all hooks to take callbacks that may mutate a ref. This is potentially unsafe if the hook eagerly calls its callback, but the alternative is excessively limiting (and inconsistent with other enforcement). This also bans *directly* passing a ref.current value to a hook, which was previously allowed. ghstack-source-id: 11024355a4e6b8cb2464da7819b52e505d327299 Pull Request resolved: #30917
- Loading branch information
Showing
9 changed files
with
238 additions
and
51 deletions.
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 |
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}], | ||
}; |