Introduction
This article came about because a student asked about the difference between watch and useWatch during a course. When I first answered, I only skimmed the documentation. Later, I realized that my answer had misled the student, so I wrote this article to document the differences in detail. Hopefully, I won’t make the same mistake again 😐.
What Is watch Used For? Practical Examples
Watches the specified inputs and returns their values. It is useful for rendering input values and deciding what to render based on certain conditions.
In short, the official description boils down to: “Watch for form changes and perform certain actions when they occur.” But when would you actually need this method? In addition to the practical example provided in the official documentation, I’ll walk through implementations of two documented usage patterns here.
Example 1: Displaying the Appropriate Payment Fields for the Selected Payment Method
You can use watch to watch for changes to the paymentMethod value: display the cash-on-delivery fields when cash on delivery is selected, the credit card fields when credit card payment is selected, the bank transfer fields when bank transfer is selected, and so on.
const { register, handleSubmit, watch } = useForm({ defaultValues: { paymentMethod: 'cashOnDelivery', },});
// Watch the hasCreditCard value in the form and destructure the resultconst [paymentMethod] = watch(['paymentMethod']);
return ( <form onSubmit={handleSubmit((data) => console.log(data))}> <label> Payment method: <select {...register('paymentMethod')}> <option value="cashOnDelivery">Cash on delivery</option> <option value="creditCard">Credit card</option> </select> </label> {paymentMethod === 'creditCard' && <div>Credit card payment details…</div>} {paymentMethod === 'cashOnDelivery' && <div>Cash-on-delivery payment details…</div>} </form>);Besides watching a single field as shown above, you can also watch multiple fields, as in the following example.
const watchAllFields = watch(); // Watch all changes when no arguments are providedconst watchFields = watch(['firstName', 'lastName']); // Specify which values to watchwatch essentially handles the combination of useState and onChange for us. Simply pass in the fields you want to watch, and you can track form changes and perform actions when they occur. This approach can make code more concise and easier to understand in complex forms.
// The traditional approach to building a basic controlled formconst [name, setName] = useState('');<input type="text" value={name} onChange={(e) => setName(e.target.value)} />;Example 2: Watching Without Triggering a Re-render
If you only want to watch for form changes without triggering a re-render, you can use watch with useEffect to do just that.
useEffect(() => { const subscription = watch((data) => { console.log(data); }); return () => subscription.unsubscribe();}, [watch]);Note that when you pass a callback to watch, you need to unsubscribe in the cleanup function of useEffect.
What Is useWatch Used For? Practical Examples
Similar to watch, but it isolates re-rendering at the hook level, potentially improving application performance.
As mentioned earlier, watch behaves like a combination of useState and onChange: whenever a form value changes, it triggers a re-render at the level where useForm is called. As the form grows more complex, this can become a performance bottleneck.
useWatch was created to address this problem. When a watched value changes, it isolates re-rendering to the level where useWatch is called, rather than re-rendering the root component of the entire form. Here is a practical example:
// Variables defined outside the components to track render countslet childRender = 0;let parentRender = 0;// Child component: use useWatch to watch the lastName value in the formfunction Child({ control }) { useWatch({ control, name: 'lastName', });
childRender += 1;
return <p>Child render count: {childRender}</p>;}
// Parent component: use useWatch to watch the firstName value in the formfunction Parent() { const { register, control } = useForm(); useWatch({ name: 'firstName', control, });
parentRender += 1;
return ( <form> <input {...register('firstName')} placeholder="firstName" /> <input {...register('lastName')} placeholder="lastName" /> <p>Parent render count: {parentRender}</p> <FirstNameWatched control={control} /> </form> );}In this example, re-rendering starts at whichever component calls useWatch. As a result, changes to the value watched by the child component do not cause the parent component to re-render, improving performance.
Summary
The difference between useWatch and watch is simply the level at which they trigger re-renders. If your application has no performance issues, watch is sufficient. If performance becomes a problem, you can use useWatch to address it.