npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hook-form-subscribe

v0.2.0

Published

Imperative subscribe hook for RHF fields

Downloads

212

Readme

Introducing react-hook-form-subscribe! 🔥 Subscribe to React Hook Form field changes without re-renders 🔄 and enjoy an easy-to-use, readable API 🙌

import { FC } from 'react';
import { useForm } from 'react-hook-form';
import { useCreateFormSubscribe } from 'react-hook-form-subscribe';

const App: FC = () => {
  const { control, register } = useForm({
    defaultValues: {
      firstname: '',
      lastname: '',
    },
  });

  const useFormSubscribe = useCreateFormSubscribe(control);

  useFormSubscribe('firstname', (firstname) => console.log(firstname));
  useFormSubscribe(['firstname', 'lastname'], ([firstname, lastname]) =>
    console.log(firstname, lastname)
  );

  return (
    <form>
      <input {...register('firstname')} />
      <input {...register('lastname')} />
    </form>
  );
};

Examples

Installation

npm install react-hook-form-subscribe --save
yarn add react-hook-form-subscribe
pnpm add react-hook-form-subscribe

Usage

react-hook-form-subscribe exposes a single hook, useCreateFormSubscribe, that allows you to subscribe to changes on any controlled form field.

useCreateFormSubscribe

useCreateFormSubscribe takes a single argument, the control object from react-hook-form. It returns a function that you can use to subscribe to changes on the form fields.

Arguments

  • control (Object): The control object from react-hook-form. Return value
  • useFormSubscribe (Function): A function that you can use to subscribe to changes on the form fields.
import { useCreateFormSubscribe } from 'react-hook-form-subscribe';
...
const useFormSubscribe = useCreateFormSubscribe(control);

useFormSubscribe

useFormSubscribe is a the returned function from useCreateFormSubscribe that you can use to subscribe to changes on the form fields. It takes two arguments: the name of the field you want to subscribe to, and a callback function that will be called when the field changes.

Arguments

  • name (String | Array): The name of the field you want to subscribe to. You can pass a single field name as a string, or an array of field names.
  • callback (Function): A callback function that will be called when the field changes. The callback function will be passed the new value of the field.
useFormSubscribe('firstname', (firstname) => console.log(firstname));
useFormSubscribe(['firstname', 'lastname'], ([firstname, lastname]) =>
  console.log(firstname, lastname)
);

In the example above, we subscribe to changes on the firstname field and log the new value to the console. We also subscribe to changes on the firstname and lastname fields and log their new values to the console.

Experimental API

useCreateTrackedFormSubscribe

Caution the useCreateTrackedFormSubscribe is still experimental. Use it at your own risk.

useCreateTrackedFormSubscribe is an experimental API that allows you to subscribe to changes without specifying the field names and instead it tracks accessed fields automatically via a es proxy.

By the nature of this different method of tracking fields, the callback will be called for first name if any field of the form changes, which establishes the baseline for the tracked fields.

To mitigate the quirks and overhead of proxy objects only this first call is tracked via the proxy object and the subsequent calls will not influence which fields are tracked.

This means that if the fields you access in the callback very from call to call, they are not properly tracked.

Bad example

Demonstrated by this example where the callback accesses different fields based on a random number. The first call will determine which field is tracked.

import { unstable_useCreateTrackedFormSubscribe as useCreateTrackedFormSubscribe } from 'react-hook-form-subscribe';
...
const useTrackedFormSubscribe = useCreateTrackedFormSubscribe(control);
...
useTrackedFormSubscribe(({firstname, lastname}) =>
  if(Math.random() > 0.5) {
    console.log(firstname)
  } else {
    console.log(firstname)
  }
);

useCreateTrackedFormSubscribe

useCreateTrackedFormSubscribe takes a single argument, the control object from react-hook-form. It returns a function that you can use to subscribe to changes on the form fields.

Arguments

  • control (Object): The control object from react-hook-form. Return value
  • useTrackedFormSubscribe (Function): A function that you can use to subscribe to changes on the form fields.
import { unstable_useCreateTrackedFormSubscribe as useCreateTrackedFormSubscribe } from 'react-hook-form-subscribe';
...
const useTrackedFormSubscribe = useCreateTrackedFormSubscribe(control);

useTrackedFormSubscribe

useTrackedFormSubscribe is a the returned function from useTrackedFormSubscribe that you can use to subscribe to changes on the form fields. It only takes one argument, a callback function that will be called when a tracked field changes.

Arguments

  • callback (Function): A callback function that will be called when the field changes. The callback function will be passed the new value of the field.
useTrackedFormSubscribe(({firstname}) => console.log(firstname));
useTrackedFormSubscribe(({firstname, lastname}) =>
  console.log(firstname, lastname)
);

License

react-hook-form-subscribe is released under the MIT License. See LICENSE for details.