Coder Social home page Coder Social logo

Comments (4)

josephsavona avatar josephsavona commented on July 27, 2024 1

Thanks for filing this. If i'm understanding correctly, the issue is with the following two lines:

const form = useForm({ defaultValues: { customerId: "", addressId: "" } });
const customerId = form.watch("customerId");

Where the customerId isn't updating when React Compiler is applied.

React Compiler is applying the equivalent of the following to this code:

const form = useForm({ defaultValues: { customerId: "", addressId: "" } });
const customerId = useMemo(() => form.watch("customerId"), [form]);

If you do the same, and apply useMemo (without the compiler), does customerId update as you'd expect? Note that React components must be pure/idempotent. If props, state, and context haven't changed, then the component is expected to return the same result. Since form is the same, it's expected that customerId would be the same too. So it would help to understand if customerId updates if you use the useMemo version without compiler applied.

from react.

erikshestopal avatar erikshestopal commented on July 27, 2024 1

Got it - thanks! Will close out then since this works as expected when using useWatch.

from react.

erikshestopal avatar erikshestopal commented on July 27, 2024

@josephsavona Thanks for the quick response.

If you do the same, and apply useMemo (without the compiler), does customerId update as you'd expect?

const form = useForm({ defaultValues: { customerId: "", addressId: "" } });
const customerId = useMemo(() => form.watch("customerId"), [form]);

If I apply this change manually without the compiler enabled, I still get the same result (customerId not being updated) because as you mentioned, form remains the same across re-renders.

I can get around issue using the useWatch hook react-hook-form provides but it was unexpected that the behavior changed with the compiler enabled.

from react.

josephsavona avatar josephsavona commented on July 27, 2024

Thanks for confirming. As I noted above, React expects components to be idempotent and only update if their props, state, or context changes. It looks like the watch() API isn't using state to tell React that something has changed, which goes against React's rules around idempotency.

The useWatch() API looks like it does use internal state to tell React when changes occur, so that should work just fine (both with useMemo and React Compiler).

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.