Skip to content

useWatch as a component #13087

@poulet42

Description

@poulet42

Is your feature request related to a problem? Please describe.

It happens quite often in dynamic forms that some field is displayed / hidden based on the value of another field. To implement something like this, we can either use the watch return from useForm or the useWatch hook.

The issue with using watch is that the re-renders are not isolated to only the component that needs the data.

useWatch allow us to subscribe to a value and isolate re-render, but we have to extract the conditional rendered field in its own component, making it unclear by looking at the form one field will be displayed / hidden based on another field value.

Describe the solution you'd like

It would be nice for RHF to also expose a Watch component, which would be a thin wrapper around the useWatch hook, but allow to compose without having to break down in multiple components:

const { register } = useForm({ ... });

 return (
  <form>
    <input type="checkbox" {...register('hasPet')} />
    <Watch name="hasPet">
      {(hasPet) => hasPet ? <input type="text" {...register('petName') /> : null}
    </Watch>
  </form>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions