-
Notifications
You must be signed in to change notification settings - Fork 46.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Allow functions containing refs to be returned
Summary: We previously were excessively strict about preventing functions that access refs from being returned--doing so is potentially valid for hooks, because the return value may only be used in an event or effect. ghstack-source-id: cfa8bb1b54e8eb365f2de50d051bd09e09162d7b Pull Request resolved: #30724
- Loading branch information
Showing
4 changed files
with
90 additions
and
57 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
37 changes: 0 additions & 37 deletions
37
...ct-compiler/src/__tests__/fixtures/compiler/error.return-ref-callback.expect.md
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
...in-react-compiler/src/__tests__/fixtures/compiler/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,55 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @flow @validateRefAccessDuringRender @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {useRef} from 'react'; | ||
|
||
component Foo() { | ||
const ref = useRef(); | ||
|
||
const s = () => { | ||
return ref.current; | ||
}; | ||
|
||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
|
||
import { useRef } from "react"; | ||
|
||
function Foo() { | ||
const $ = _c(1); | ||
const ref = useRef(); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = () => ref.current; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
const s = t0; | ||
return s; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Foo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) "[[ function params=0 ]]" |
2 changes: 2 additions & 0 deletions
2
...res/compiler/error.return-ref-callback.js → .../fixtures/compiler/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