1.0.0 ā€¢ Published 5 years ago

@zhuangya/react-final-form-hooks v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

šŸ 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.22 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

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.

useForm

Returns an object similar to FormRenderProps.


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]