Skip to content

Commit cfeae4c

Browse files
committed
[compiler] Ref validation repro for ImportSpecifier with renamed local
This was originally reported in reactwg/react-compiler#27. Adding a failing repro to capture this case.
1 parent 0bc3074 commit cfeae4c

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
## Input
3+
4+
```javascript
5+
import {
6+
useEffect,
7+
useRef,
8+
// @ts-expect-error
9+
experimental_useEffectEvent as useEffectEvent,
10+
} from 'react';
11+
12+
let id = 0;
13+
function uniqueId() {
14+
'use no memo';
15+
return id++;
16+
}
17+
18+
export function useCustomHook(src: string): void {
19+
const uidRef = useRef(uniqueId());
20+
const destroyed = useRef(false);
21+
const getItem = (srcName, uid) => {
22+
return {srcName, uid};
23+
};
24+
25+
const getItemEvent = useEffectEvent(() => {
26+
if (destroyed.current) return;
27+
28+
getItem(src, uidRef.current);
29+
});
30+
31+
useEffect(() => {
32+
destroyed.current = false;
33+
getItemEvent();
34+
}, []);
35+
}
36+
37+
function Component() {
38+
useCustomHook('hello');
39+
return <div>Hello</div>;
40+
}
41+
42+
export const FIXTURE_ENTRYPOINT = {
43+
fn: Component,
44+
isComponent: true,
45+
params: [{x: 1}],
46+
};
47+
48+
```
49+
50+
51+
## Error
52+
53+
```
54+
19 | };
55+
20 |
56+
> 21 | const getItemEvent = useEffectEvent(() => {
57+
| ^^^^^^^
58+
> 22 | if (destroyed.current) return;
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
> 23 |
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62+
> 24 | getItem(src, uidRef.current);
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
> 25 | });
65+
| ^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (21:25)
66+
26 |
67+
27 | useEffect(() => {
68+
28 | destroyed.current = false;
69+
```
70+
71+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
useEffect,
3+
useRef,
4+
// @ts-expect-error
5+
experimental_useEffectEvent as useEffectEvent,
6+
} from 'react';
7+
8+
let id = 0;
9+
function uniqueId() {
10+
'use no memo';
11+
return id++;
12+
}
13+
14+
export function useCustomHook(src: string): void {
15+
const uidRef = useRef(uniqueId());
16+
const destroyed = useRef(false);
17+
const getItem = (srcName, uid) => {
18+
return {srcName, uid};
19+
};
20+
21+
const getItemEvent = useEffectEvent(() => {
22+
if (destroyed.current) return;
23+
24+
getItem(src, uidRef.current);
25+
});
26+
27+
useEffect(() => {
28+
destroyed.current = false;
29+
getItemEvent();
30+
}, []);
31+
}
32+
33+
function Component() {
34+
useCustomHook('hello');
35+
return <div>Hello</div>;
36+
}
37+
38+
export const FIXTURE_ENTRYPOINT = {
39+
fn: Component,
40+
isComponent: true,
41+
params: [{x: 1}],
42+
};

0 commit comments

Comments
 (0)