
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
react-final-form-hooks
Advanced tools
React Hooks to bind to š Final Form's high performance subscription-based form state management engine
ā Zero dependencies
ā Only peer dependencies: React and š Final Form
ā Opt-in subscriptions - only update on the state you need!
ā š„ 1.2 kB gzipped š„
npm install --save react-final-form-hooks final-form
or
yarn add react-final-form-hooks final-form
š 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.
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>
)
}
react-final-form-hooks
and the hooks introduced in react-final-form v5
?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
.
Shows how to create fields and attach them to <input/>
elements.
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.
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our backers! š [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
FAQs
React Hooks to bind to š Final Form's high performance subscription-based form state management engine
The npm package react-final-form-hooks receives a total of 1,093 weekly downloads. As such, react-final-form-hooks popularity was classified as popular.
We found that react-final-form-hooks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.