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 🙏

© 2025 – Pkg Stats / Ryan Hefner

form-hooks

v0.7.0

Published

Easy forms in React with hooks

Downloads

26

Readme

form-hooks

Easily create forms in React components -- with hooks! Essentially a dumbed down version of Formik using hooks.

Getting Started

npm install form-hooks
import { useForm } from 'form-hooks';

const Sample = () => (
    const {
        errors,
        touched,
        values,
        handleBlur,
        handleChange,
        handleSubmit,
        isSubmitting,
    } = useForm({
        initialValues: {
            name: '',
            email: '',
        },
        onSubmit: values => fetch(/* with values */),
        validate: values => ({
            ...(!values.name.length ? { name: 'Requires a name' } : {}),
            ...(!values.email.length ? { email: 'Requires an email' } : {})
        }),
    }, options => [options.initialValues]);

    return (
        <form onSubmit={handleSubmit}>
            <input name="name"
                   type="text"
                   value={values.name}
                   onChange={handleChange}
                   onBlur={handleBlur}
            />
            {touched['name'] && errors['name']}
            <input name="email"
                   type="text"
                   value={values.email}
                   onChange={handleChange}
                   onBlur={handleBlur}
            />
            {touched['email'] && errors['email']}
            <button type="submit" disabled={isSubmitting}>submit</button>
        </form>
    )
)

Documentation

useForm<Values>(options: FormHookOptions, dependencies?: FormHookDependencies<Values>): FormHookState<Values> - FormHookOptions

The useForm hook takes some options (as an object) to initialize state and manage form validation/submissions.

initialValues: Values

An object with the forms initial values. These values should not be required.

onSubmit: (values: Values) => void

Called when a form is submitted with the values set. Only called if validation passes.

validate: (values: Values) => FormHookErrors<Values> | Promise<FormHookErrors<Values>>

Called when a form is submitted prior to the onSubmit call. Returns an object of errors similar to Formik.

validateOnBlur: boolean - true

Indicates if useForm should re-validate the input on blur. Only fired when all fields have been touched that were in the initialValues object.

validateOnChange: boolean - true

Indicates if useForm should re-validate the input on change. Only fired when all fields have been touched that were in the initialValues object.

useForm<Values>(options: FormHookOptions, dependencies?: FormHookDependencies<Values>): FormHookState<Values> - FormHookState

errors: FormHookErrors<Values>

An object that contains the form errors where the key is the field name and the value is the error message.

touched: FormHookTouched<Values>

An object that contains which form fields have been touched. The key is the field name, the value is a boolean of if the field has been touched.

values: FormHookValues

An object that contains all of the values of the form. Initialized with the initialValues originally passed in. Modified by the handleChange handler.

handleBlur: (event: React.ChangeEvent<any>) => void

Marks a field as touched to show errors after all fields are touched.

handleChange: (event: React.ChangeEvent<any>) => void

Changes the fields value in the values state.

handleSubmit: (event: React.ChangeEvent<HTMLFormElement>) => Promise<void>

Handles calling validation prior to the onSubmit handler and setting the touched, errors and isSubmitting state internally.

isSubmitting: boolean

Boolean value if the form is currently submitting.

submitCount: number

Number of times the form was submitted.

setErrors: (errors: FormHookErrors<Values>) => void

Function that allows for errors to be set outside of the useForm internal handlers (good for handling request errors).

setTouched: (name: keyof Values, touched?: boolean = true)

Sets a field to touched or untouched. Triggers a re-validation if validateOnBlur is enabled and all fields have been touched.

setValue: (name keyof Values, value: any)

Sets a fields value. Re-valdiates if validateOnChange is enabled and all required conditions are met.

resetForm: () => void

Resets a form to its initial state.

resetValue: (name: keyof Values, shouldResetTouched?: boolean = false)

Resets a field value to its initial value and resets its errors state. Can optionally reset its touched state.

FormHookDependencies<Values> - Form Reinitialization

The second parameter to useForm allows for a list of dependencies to be declared from the collection of options passed through in the first argument. A change in a dependency will reset the form. For instance in this example:

useForm(
  {
    initialValues: { first: 'John', last: 'Doe' },
    onSubmit: () => {},
    validate: () => ({}),
  },
  options => [options.initialValues]
);

Changing the initialValues object will cause the Form to be re-initialized. initialValues, errors, touched, submitCount and isSubmitting will be reset.