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

  1. Clone https://github.com/Maxou44/issue-react-compiler-react-hook-form
  2. Run npm i --force && npm run start
  3. Open a web browser on http://localhost:5173/
  4. 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

Most upvoted comments

@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 useMemo with watch will 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() with useWatch()

  const { control, watch } = useForm();
- const field = watch('field');
+ const field = useWatch({ name: 'field', control })

Option 2: opt out of optimization with use no memo for components using watch()

  const Component = () => {
+   'use no memo'
    const { control, watch } = useForm();
    const field = watch('field');
  }

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