Open nmay231 opened 1 day ago
Thanks for posting. It looks like the useProxy() API specifically is breaking React’s rules. Normal Valtio usage per the website should be fine - w proxy() calls outside components and then useSnapshot() to get an immutable snapshot for rendering.
The problem is specifically with useProxy(), which is a mutable value. This is probably worth posting about as a Valtio issue.
What kind of issue is this?
Link to repro
https://github.com/nmay231/eslint-plugin-react-compiler-bug
Repro steps
How to see issue with given repo:
When you pass a valtio proxy object to
useProxy
, it is safe to modify the resulting proxy object directly as a way tosetState
. Peeking atUnoptimized.tsx
, in particular the buttononClick
handler:The problem
The compiler (and eslint plugin) fail to realize this is safe to optimize, aka this is a false negative. One workaround is to use
const state = useSnapshot(myProxy)
for reading from the proxy and to modify the proxy object directly (myProxy.attr = ...
). Another is to manually optimize withReact.memo
where proxy objects are used, but that feels counter-productive.The real question is, Is there a way for valtio developers to mark
useProxy
's return value as safe to mutate?Thanks.
Steps to recreate repo from scratch
1. `pnpm create vite` 4. `pnpm add -D eslint-plugin-react-compiler@beta` 5. `pnpm add react-compiler-runtime@beta babel-plugin-react-compiler@beta valtio` 6. Update eslint config to use plugin, and `vite.config.ts` to use babel plugin (skimming though [this guide](https://jherr2020.medium.com/react-compiler-with-react-18-1e39f60ae71a) helped me). 7. Create a proxy object, and use the proxy object using valtio's `useProxy` hook. 8. Modifying the proxy object should be safe to do, but the compiler rules are too simple to realize that it is safe.How often does this bug happen?
Every time
What version of React are you using?
18.3.1
What version of React Compiler are you using?
19.0.0-beta-8a03594-20241020