• Stars
    star
    471
  • Rank 93,216 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React Hooks to bind to 🏁 Final Form's high performance subscription-based form state management engine

🏁 React Final Form Hooks

React Final Form Hooks

Backers on Open Collective Sponsors on Open Collective NPM Version NPM Downloads Build Status codecov.io styled with prettier

βœ… Zero dependencies

βœ… Only peer dependencies: React and 🏁 Final Form

βœ… Opt-in subscriptions - only update on the state you need!

βœ… πŸ’₯ 1.2 kB gzipped πŸ’₯


Installation

npm install --save react-final-form-hooks final-form

or

yarn add react-final-form-hooks final-form

Getting Started

🏁 React Final Form Hooks is the leanest possible way to connect 🏁 Final Form to React, to acheive subscriptions-based form state management using the Observer pattern.

⚠️ This library will re-render your entire form on every state change, as you type. ⚠️

If performance is your goal, you are recommended to use 🏁 React Final Form. Also, that library does many other things for you, like managing checkbox and radio buttons properly. RFFHooks leaves all of that work to you. By default, 🏁 React Final Form Hooks subscribes to all changes, but if you want to fine tune your form, you may specify only the form state that you care about for rendering your gorgeous UI.

Here's what it looks like in your code:

import { useForm, useField } from 'react-final-form-hooks'

const MyForm = () => {
  const { form, handleSubmit, values, pristine, submitting } = useForm({
    onSubmit, // the function to call with your form values upon valid submit
    validate // a record-level validation function to check all form values
  })
  const firstName = useField('firstName', form)
  const lastName = useField('lastName', form)
  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>First Name</label>
        <input {...firstName.input} placeholder="First Name" />
        {firstName.meta.touched && firstName.meta.error && (
          <span>{firstName.meta.error}</span>
        )}
      </div>
      <div>
        <label>Last Name</label>
        <input {...lastName.input} placeholder="Last Name" />
        {lastName.meta.touched && lastName.meta.error && (
          <span>{lastName.meta.error}</span>
        )}
      </div>
      <button type="submit" disabled={pristine || submitting}>
        Submit
      </button>
    </form>
  )
}

Table of Contents

What's the difference between react-final-form-hooks and the hooks introduced in react-final-form v5?

Great question. The TL;DR is this:

  • react-final-form-hooks is a lightweight, simple solution for quickly getting a form up and running in a single render function, but allows for no performance optimization.
  • react-final-form v5 is a more robust, battle-tested solution that involves creating more components and structure around your form.

react-final-form-hooks does not put the form instance into the React context, but rather forces you to pass the form instance to useField so that the field can register itself with the form. This allows you to create your entire form in a single functional component, like the MyForm example above. It will also, by necessity, rerender your entire form on every value change.

react-final-form v5 requires that you wrap your entire form in a <Form> component that provides the form instance via context to its descendants. This means that you cannot use useField in the same function that is rendering your <Form>, because useField must be inside the <Form>.

Conclusion: If your app has a couple of small (< 20 inputs) forms where you aren't doing anything fancy with reusable custom input components, react-final-form-hooks might be all you need. But if your app is bigger and more sophisticated, or you need to optimize for performance, you should probably use react-final-form.

Examples

Simple Example

Shows how to create fields and attach them to <input/> elements.

API

The following can be imported from react-final-form-hooks.

useField

Returns an object similar to FieldRenderProps.

useField takes four parameters:

name : string

The name of the field. Required.

form : Form

The object returned from useForm. Required.

validate? : (value:any) => any

A field-level validation function that takes the current value and returns undefined if it is valid, or the error if it is not. Optional.

subscription? : FieldSubscription

A subscription of which parts of field state to be notified about. See FieldSubscription. Optional.

useForm

Returns an object similar to FormRenderProps.

useForm takes two parameters:

onSubmit : (values:Object) => ?Object | Promise<?Object> | void

See 🏁 Final Form's onSubmit docs for more information. Required.

validate?: (values:Object) => Object | Promise<Object>

A record level validation function. See 🏁 Final Form's validate docs for more information. Optional.


Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! πŸ™ [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

More Repositories

1

react-final-form

🏁 High performance subscription-based form state management for React
JavaScript
7,376
star
2

final-form

🏁 Framework agnostic, high performance, subscription-based form state management
JavaScript
2,992
star
3

react-final-form-arrays

A component for rendering and editing arrays 🏁 React Final Form
JavaScript
204
star
4

final-form-calculate

Decorator for calculating field values based on other field values in 🏁 Final Form
JavaScript
113
star
5

react-final-form-listeners

A collection of components to listen to 🏁 React Final Form fields
JavaScript
93
star
6

final-form-focus

🏁 Final Form "decorator" that will attempt to apply focus to the first field with an error upon an attempted form submission
JavaScript
83
star
7

rff-cli-example

An example of how to use 🏁 React Final Form in a CLI application with Ink
JavaScript
77
star
8

final-form-arrays

Array Mutators for 🏁 Final Form
JavaScript
70
star
9

react-final-form-html5-validation

A swap-in replacement for 🏁 React Final Form's <Field> component to provide HTML5 Validation
JavaScript
57
star
10

final-form-set-field-data

Mutator for setting arbitrary metadata on fields in 🏁 Final Form
JavaScript
39
star
11

builder-demo

A demonstration of how one form can be used to build another in realtime
SCSS
18
star
12

final-form-set-field-touched

Mutator for setting a field as "touched" in 🏁 Final Form
JavaScript
17
star
13

final-form-submit-listener

🏁 Final Form decorator that provides callback hooks for submitting
JavaScript
11
star