react-hook-form: issue: Watch don't work with React Compiler (React 19)
Version Number
7.51.4
Codesandbox/Expo snack
https://github.com/Maxou44/issue-react-compiler-react-hook-form
Steps to reproduce
- Clone
https://github.com/Maxou44/issue-react-compiler-react-hook-form - Run
npm i --force && npm run start - Open a web browser on
http://localhost:5173/ - Type something in the input field, the watched value is not displayed on the page
To disable the React compiler and confirm it works without the compiler, comment the line plugins: [["babel-plugin-react-compiler", ReactCompilerConfig]], in vite.config.mjs, relaunch npm start and type something in the input field
Expected behaviour
The use of React Compiler don’t allow the use of the watch() function, it should work.
What browsers are you seeing the problem on?
Firefox, Chrome, Safari, Edge
Relevant log output
No response
Code of Conduct
- I agree to follow this project’s Code of Conduct
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 24 (5 by maintainers)
Commits related to this issue
- ☸️ fix #11910 watch reference update on each re-render (#12267) * fix watch reference update on each re-render * include unit test coverage — committed to bombillazo/rhf-plus by bluebill1049 2 years ago
- ☸️ fix #11910 watch reference update on each re-render (#12267) * fix watch reference update on each re-render * include unit test coverage — committed to oskarski/react-hook-form by bluebill1049 2 years ago
- Revert "☸️ fix #11910 watch reference update on each re-render (#12267)" This reverts commit b5863b46346416972c025f4b621cb624ffc4a955. — committed to react-hook-form/react-hook-form by bluebill1049 2 years ago
- Revert "☸️ fix #11910 watch reference update on each re-render (#12267)" (#12312) This reverts commit b5863b46346416972c025f4b621cb624ffc4a955. — committed to react-hook-form/react-hook-form by bluebill1049 2 years ago
- Revert "Revert "☸️ fix #11910 watch reference update on each re-render (#12267)" (#12312)" This reverts commit 3bce0c8f3a8a02e2206d23c89e523919cfa5d952. — committed to react-hook-form/react-hook-form by bluebill1049 2 years ago
- 🏺 watch reference update on formState update (#12326) * Revert "Revert "☸️ fix #11910 watch reference update on each re-render (#12267)" (#12312)" This reverts commit 3bce0c8f3a8a02e2206d23c89e52... — committed to react-hook-form/react-hook-form by bluebill1049 2 years ago
- fix: minor bug fixes - https://github.com/react-hook-form/react-hook-form/issues/11910 — committed to ISnackable/saf-duty by ISnackable 2 years ago
@Maxou44 thanks for reporting this here! For folks working on react-hook-form, we’ve written up some guidelines for how to think about designing libraries for maximum compatibility with React, including React Compiler.
As @bluebill1049 notes, using
useMemowithwatchwill cause results to be stale. This is part of a pattern, where non-hook APIs should always be memoizable. We cover this in https://github.com/reactwg/react-compiler/discussions/10 (“Any non-hook usage of your library should be memoize-able” section). Please let us know if you have questions!@bluebill1049 @josephsavona So can I conclude that, at this stage, if we still want/need to use
watch()with React Compiler enabled, there are two workarounds:Option 1: replace
watch()withuseWatch()Option 2: opt out of optimization with
use no memofor components usingwatch()Is that correct?
I guess “use no memo” can be used temporarily? Currently trying to wrangler with Shadcn Form, RHF and the compiler. Looks like memoization in RHF is the main issue, not sure though
thanks, @josephsavona makes sense. I will seek a solution if possible without deprecating
watch. It’s a great API for DX.What I mean is that you can choose whether to apply React Compiler to the code that you publish.
Regardless, though, your users might wrap a call to watch() in a useMemo() (always allowed in React) or use React Compiler, which will do the equivalent of wrapping watch() calls in useMemo, and that will break their apps.
Note that useWatch is compatible w useMemo and React Compiler, so it seems like you should be able to deprecate watch in favor of useWatch. Unless there’s something I’m missing! This is all just a heads up so that you have context on potential issues your users may face using the library.
The challenge is that callers of the API would need to skip compilation. For your library itself you could just not compile the library - that’s your choice! But developers are going to expect that their libraries don’t block them from adopting the compiler on their own code.
Ultimately the watch API would need to either change (so that the form object itself changes when its state changes) or be replaced w the useWatch() API.
Since React Compiler is still experimental, the bug have also been reported to React Compiler team, they replied it works as excepted and the library don’t follow the React guidelines : https://github.com/facebook/react/issues/29144