Skip to content

Comments

feat: <FormStateSubscribe /> component#13142

Merged
bluebill1049 merged 6 commits intoreact-hook-form:masterfrom
ntatoud:feat/form-state-subscribe-component
Nov 28, 2025
Merged

feat: <FormStateSubscribe /> component#13142
bluebill1049 merged 6 commits intoreact-hook-form:masterfrom
ntatoud:feat/form-state-subscribe-component

Conversation

@ntatoud
Copy link
Contributor

@ntatoud ntatoud commented Nov 10, 2025

Documented in

react-hook-form/documentation#1179

Proposal

This is a follow up to the <Watch /> component addition.

The component in itself isn't that complex. The main idea is to keep a consistent API between useWatch and useFormState

The <FormStateSubscribe /> component is merely a wrapper around the useFormState logic.

The name "FormStateSubscribe" was chosen to keep the component's intention clear since "FormState" is already an existing type.

Currently

import { useForm, useFormState } from 'react-hook-form';

const FooSubscriber = ({ control }) => {
  const {errors} = useFormState({ control, name: 'foo' });
  return <span>{errors.foo?.message}</span>;
};

const App = () => {
  const { register, control } = useForm();

  return (
    <div>
      <form>
        <input {...register('foo')} />
        <input {...register('bar')} />
      </form>
      {/* re-render only when formState of `foo` changes */}
      <FooSubscriber control={control} />
    </div>
  );
};

Goal

import { useForm, FormStateSubscribe } from 'react-hook-form';

const App = () => {
  const { register, control } = useForm();

  return (
    <div>
      <form>
        <input {...register('foo')} />
        <input {...register('bar')} />
      </form>
      {/* re-render only when formState of `foo` changes */}
      <FormStateSubscribe
        control={control}
        name={"foo"}
        render={({errors}) => <span>{errors.foo?.message}</span>}
      />
    </div>
  );
};
  • The props and behavior are the same as useFormState, added the render prop.
  • The types of render function's arguments are accurately inferred.

@bluebill1049
Copy link
Member

nice, i think we can just name it as <FormState /> shorter to type.

@ntatoud
Copy link
Contributor Author

ntatoud commented Nov 10, 2025

@bluebill1049 as mentionned, FormState is already exported as a type ☹️ (defined here: https://github.com/react-hook-form/react-hook-form/blob/master/src/types/form.ts)

Could be changing the FormState type export name to FormStateType but it also feels weird.

@bluebill1049
Copy link
Member

Ah, my bad, I misread your comment!

bluebill1049
bluebill1049 previously approved these changes Nov 10, 2025
Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ntatoud ntatoud force-pushed the feat/form-state-subscribe-component branch from cef7d57 to 8f62e3b Compare November 10, 2025 22:55
@ntatoud
Copy link
Contributor Author

ntatoud commented Nov 11, 2025

#13142 (comment)
@bluebill1049 I had to bump the maxSize for bundlewatch, if you're fine fine with that I think we're all set 😊

@bluebill1049
Copy link
Member

I will release this a bit later, as i would need to update the doc on this before publish as well. 🙏

@ntatoud
Copy link
Contributor Author

ntatoud commented Nov 25, 2025

I will release this a bit later, as i would need to update the doc on this before publish as well. 🙏
I opened a PR for that too the other day 😉 : react-hook-form/documentation#1179

@bluebill1049
Copy link
Member

I will release this a bit later, as i would need to update the doc on this before publish as well. 🙏
I opened a PR for that too the other day 😉 : react-hook-form/documentation#1179

wow thanks!

@bluebill1049 bluebill1049 merged commit 43bcf05 into react-hook-form:master Nov 28, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants