Skip to content

Commit 9f141fa

Browse files
committed
Update on "[compiler] Type ref prop as a ref"
Adds a shape type for component props, which has one defined property: "ref". This means that if the ref property exists, we can type usage of `props.ref` (or via destructuring) the same as the result of `useRef()` and infer downstream usage similarly. [ghstack-poisoned]
1 parent dae5020 commit 9f141fa

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @validateRefAccessDuringRender @compilationMode(infer)
6+
function Component(props) {
7+
const ref = props.ref;
8+
ref.current = true;
9+
return <div>{value}</div>;
10+
}
11+
12+
```
13+
14+
15+
## Error
16+
17+
```
18+
2 | function Component(props) {
19+
3 | const ref = props.ref;
20+
> 4 | ref.current = true;
21+
| ^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (4:4)
22+
5 | return <div>{value}</div>;
23+
6 | }
24+
7 |
25+
```
26+
27+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @validateRefAccessDuringRender @compilationMode(infer)
2+
function Component(props) {
3+
const ref = props.ref;
4+
ref.current = true;
5+
return <div>{value}</div>;
6+
}

0 commit comments

Comments
 (0)