Skip to content

Commit

Permalink
[compiler] Fixture to show ref-in-render enforcement issue with useCa…
Browse files Browse the repository at this point in the history
…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
mvitousek committed Aug 16, 2024
1 parent 5030e08 commit 7468ac5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
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 = {
```
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: [],
};
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 | }
```
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: [],
};

0 comments on commit 7468ac5

Please sign in to comment.