Repository navigation
Description
What kind of issue is this?
- React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
- babel-plugin-react-compiler (build issue installing or using the Babel plugin)
- eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
- react-compiler-healthcheck (build issue installing or using the healthcheck script)
Link to repro
Repro steps
Hi! I'm hoping the React compiler catches this invalid ref access and bails out of optimizing code like this, but I understand that catching this kind of issue is probably a bigger undertaking than normal.
Our team's codebase has some dubious core library hooks which break React's rules, or at least make it easy to break React's rules without knowing what you're doing. Here, useSyncState advertises itself as a cooler useState that lets you see the freshest state, even before React does! (But after memoization, this just means you see stale state). This leads to it being used incorrectly in useEditable, when we call getValueBeingEdited() inside of a render.
When I installed the compiler on our codebase, many E2E tests started failing. One of the tests showed us that when we update one EditableCell, the state change doesn't propagate to another cell like it was supposed to. The test passed after I added 'use no memo' to either useSyncState.ts or useEditable.ts. For now, I'm going to add 'use no memo' to both of these files, but my understanding is that every time I add 'use no memo', you'd like to see why I had to do so.
How often does this bug happen?
Every time
What version of React are you using?
Playground
What version of React Compiler are you using?
Playground
Activity
jthemphill commentedon Nov 12, 2024
From the compiler team's end, it looks like the compiler team has decided to handle this problem with the
enableTreatRefLikeIdentifiersAsRefsoption.From my codebase's end, we'll want to replace
return () => ref.currentwith simplyreturn ref. We'll let consumers accessref.current, and we'll make sure that consumers name the variables coming out of these custom hooks with the...Refconvention so that the compiler can track refs across import boundaries.Unless the compiler does full-on taint analysis instead of relying on naming conventions, we're unlikely to get to a point where the compiler never breaks code.
josephsavona commentedon Jul 25, 2025
Hey @jthemphill apologies for the delayed reply on your issue, this slipped through the cracks. We're working on a number of improvements to ref validation. I think your approach in your reply is exactly right: we are adding validation to detect refs based on naming and usage of the
.currentproperty, so if you change the API to return the first value as a ref we'd detect it.jthemphill commentedon Jul 28, 2025
@josephsavona Thanks for the reply! I really appreciate your responses and I know first-hand how difficult it is to keep up with the OSS community from within Meta🙂
React already requires you to follow naming conventions for components and hooks, so I'm very happy with a solution for refs that's based on naming conventions, as long as this is called out in the https://react.dev docs!
josephsavona commentedon Jul 29, 2025
#34005 enables the flag to treat ref-like identifiers as refs by default. Thanks again for posting!