Shubham Luthra
Shubham Luthra

Reputation: 31

how do we use watch as a callback in react-hook form(v6)?

The docs for v6 of react hook form say something like -

// Callback version of watch.  It's your responsibility to unsubscribe when done.
React.useEffect(() => {
    const subscription = watch((value, { name, type }) => console.log(value, name, type));
    return () => subscription.unsubscribe();
  }, [watch]);

Can someone give me some examples of this implementation?

Upvotes: 3

Views: 4601

Answers (1)

Coco
Coco

Reputation: 906

It depends on what you want to do. But if you want to watch a specific value, you can do like this:

const value = watch('value'); // value is the name of the input from your form
console.log(value); // No need for useEffect, this will re-render whenever value is changed

Upvotes: 2

Related Questions