Skip to content

[compiler] Treat ref-like named objects as refs - #29916

Merged
gsathya merged 1 commit into
react:mainfrom
gsathya:ref-like-name
Jun 18, 2024
Merged

gsathya merged 1 commit into
react:mainfrom
gsathya:ref-like-name

Conversation

@gsathya

@gsathya gsathya commented Jun 17, 2024

Copy link
Copy Markdown
Contributor

If a component uses the useRef hook directly then we type it's return value as a ref. But if it's wrapped in a custom hook then we lose out on this type information as the compiler doesn't look at the hook definition. This has resulted in some false positives in our analysis like the ones reported in #29160 and #29196.

This PR will treat objects named as ref or if their names end with the substring Ref, and contain a property named current, as React refs.

const ref = useMyRef();
const myRef = useMyRef2();
useEffect(() => {
  ref.current = ...;
  myRef.current = ...;
})

In the above example, ref and myRef will be treated as React refs.

@vercel

vercel Bot commented Jun 17, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 18, 2024 10:14am

@josephsavona josephsavona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thoughts on checking that other properties (besides .current) are not accessed? If they are it's a good indication that the value wasn't actually a ref.

If a component uses the `useRef` hook directly then we type it's return
value as a ref. But if it's wrapped in a custom hook then we lose out
on this type information as the compiler doesn't look at the hook
definition. This has resulted in some false positives in our analysis
like the ones reported in react#29160 and react#29196.

This PR will treat objects named as `ref` or if their names end with the
substring `Ref`, and contain a property named `current`, as React refs.

```
const ref = useMyRef();
const myRef = useMyRef2();
useEffect(() => {
  ref.current = ...;
  myRef.current = ...;
})
```
In the above example, `ref` and `myRef` will be treated as React refs.
@gsathya

gsathya commented Jun 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Nice! Thoughts on checking that other properties (besides .current) are not accessed? If they are it's a good indication that the value wasn't actually a ref.

There's still going to be false positives in either case, I'm not sure if this worth the extra complexity? Lets land this and try it on our codebase to see if there are false positives?

@react-sizebot

Copy link
Copy Markdown

Comparing: 92219ff...4bb18bd

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.66 kB 6.66 kB +0.05% 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 497.93 kB 497.93 kB = 89.26 kB 89.26 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.67 kB 6.67 kB +0.05% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 502.75 kB 502.75 kB = 89.96 kB 89.96 kB
facebook-www/ReactDOM-prod.classic.js = 597.17 kB 597.17 kB = 105.33 kB 105.33 kB
facebook-www/ReactDOM-prod.modern.js = 571.52 kB 571.52 kB = 101.27 kB 101.27 kB
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
test_utils/ReactAllWarnings.js Deleted 62.88 kB 0.00 kB Deleted 15.69 kB 0.00 kB

Generated by 🚫 dangerJS against 4bb18bd

@josephsavona josephsavona left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, lets try it out

@gsathya
gsathya merged commit 83d538e into react:main Jun 18, 2024
@gsathya
gsathya deleted the ref-like-name branch June 18, 2024 17:02
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
If a component uses the `useRef` hook directly then we type it's return
value as a ref. But if it's wrapped in a custom hook then we lose out on
this type information as the compiler doesn't look at the hook
definition. This has resulted in some false positives in our analysis
like the ones reported in react#29160 and react#29196.

This PR will treat objects named as `ref` or if their names end with the
substring `Ref`, and contain a property named `current`, as React refs.

```
const ref = useMyRef();
const myRef = useMyRef2();
useEffect(() => {
  ref.current = ...;
  myRef.current = ...;
})
```
In the above example, `ref` and `myRef` will be treated as React refs.

This branch was successfully deployed

1 active deployment
Preview — 4bb18bd9 Deployed Jun 18, 2024 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Component: React Compiler React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants