Skip to content

Commit

Permalink
Update on "[compiler] Type ref prop as a ref"
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
josephsavona committed Jun 10, 2024
1 parent dae5020 commit 9f141fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

## Input

```javascript
// @validateRefAccessDuringRender @compilationMode(infer)
function Component(props) {
const ref = props.ref;
ref.current = true;
return <div>{value}</div>;
}

```


## Error

```
2 | function Component(props) {
3 | const ref = props.ref;
> 4 | ref.current = true;
| ^^^^^^^^^^^ InvalidReact: Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef) (4:4)
5 | return <div>{value}</div>;
6 | }
7 |
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @validateRefAccessDuringRender @compilationMode(infer)
function Component(props) {
const ref = props.ref;
ref.current = true;
return <div>{value}</div>;
}

0 comments on commit 9f141fa

Please sign in to comment.