Convert Figma logo to code with AI

final-form logoreact-final-form

🏁 High performance subscription-based form state management for React

7,374
481
7,374
387

Top Related Projects

A Higher Order Component using react-redux to keep form state in a Redux store

33,863

Build forms in React, without the tears 😭

📋 React Hooks for form state management and validation (Web + React Native)

3,625

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

4,465

Performance-focused API for React forms 🚀

Quick Overview

React Final Form is a high-performance, subscription-based form state management library for React applications. It provides a way to manage form state and validation with minimal re-renders, making it efficient for complex forms.

Pros

  • Minimal re-renders due to its subscription-based model
  • Lightweight and performant, especially for large forms
  • Flexible API that allows for easy customization
  • Supports both synchronous and asynchronous validation

Cons

  • Steeper learning curve compared to simpler form libraries
  • Documentation can be overwhelming for beginners
  • Less opinionated, which may require more setup for common use cases
  • Smaller community compared to some other popular form libraries

Code Examples

  1. Basic form setup:
import { Form, Field } from 'react-final-form'

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="firstName" component="input" placeholder="First Name" />
        <Field name="lastName" component="input" placeholder="Last Name" />
        <button type="submit">Submit</button>
      </form>
    )}
  />
)
  1. Form with validation:
import { Form, Field } from 'react-final-form'

const validate = values => {
  const errors = {}
  if (!values.username) {
    errors.username = 'Required'
  }
  return errors
}

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    validate={validate}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="username">
          {({ input, meta }) => (
            <div>
              <input {...input} placeholder="Username" />
              {meta.error && meta.touched && <span>{meta.error}</span>}
            </div>
          )}
        </Field>
        <button type="submit">Submit</button>
      </form>
    )}
  />
)
  1. Form with conditional fields:
import { Form, Field } from 'react-final-form'

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    render={({ values }) => (
      <form onSubmit={handleSubmit}>
        <Field name="employmentStatus" component="select">
          <option value="employed">Employed</option>
          <option value="unemployed">Unemployed</option>
        </Field>
        {values.employmentStatus === 'employed' && (
          <Field name="employer" component="input" placeholder="Employer" />
        )}
        <button type="submit">Submit</button>
      </form>
    )}
  />
)

Getting Started

To use React Final Form in your project:

  1. Install the package:

    npm install react-final-form final-form
    
  2. Import and use in your React component:

    import { Form, Field } from 'react-final-form'
    
    const MyForm = () => (
      <Form
        onSubmit={onSubmit}
        render={({ handleSubmit }) => (
          <form onSubmit={handleSubmit}>
            <Field name="username" component="input" placeholder="Username" />
            <button type="submit">Submit</button>
          </form>
        )}
      />
    )
    
  3. Define your onSubmit function to handle form submission.

Competitor Comparisons

A Higher Order Component using react-redux to keep form state in a Redux store

Pros of redux-form

  • Deeply integrated with Redux, providing a centralized state management solution
  • Extensive documentation and large community support
  • Offers more advanced features like asynchronous validation and dynamic form generation

Cons of redux-form

  • Heavier bundle size due to Redux dependency
  • Steeper learning curve, especially for developers new to Redux
  • Performance can be impacted in large applications with complex forms

Code Comparison

redux-form:

import { reduxForm, Field } from 'redux-form';

const MyForm = ({ handleSubmit }) => (
  <form onSubmit={handleSubmit}>
    <Field name="username" component="input" type="text" />
    <button type="submit">Submit</button>
  </form>
);

export default reduxForm({ form: 'myForm' })(MyForm);

react-final-form:

import { Form, Field } from 'react-final-form';

const MyForm = () => (
  <Form
    onSubmit={onSubmit}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="username" component="input" type="text" />
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Both libraries provide similar functionality for form management in React applications. redux-form is more suitable for large-scale applications already using Redux, while react-final-form is lighter and more flexible, making it a good choice for smaller projects or those not relying on Redux.

33,863

Build forms in React, without the tears 😭

Pros of Formik

  • Larger community and ecosystem, with more third-party extensions and integrations
  • Built-in support for Yup schema validation
  • More opinionated API, which can lead to faster development for some use cases

Cons of Formik

  • Heavier bundle size compared to React Final Form
  • Less flexible for complex form scenarios or custom implementations
  • Steeper learning curve for developers new to the library

Code Comparison

Formik:

import { Formik, Form, Field } from 'formik';

<Formik initialValues={{ name: '' }} onSubmit={handleSubmit}>
  <Form>
    <Field name="name" />
    <button type="submit">Submit</button>
  </Form>
</Formik>

React Final Form:

import { Form, Field } from 'react-final-form';

<Form onSubmit={handleSubmit} render={({ handleSubmit }) => (
  <form onSubmit={handleSubmit}>
    <Field name="name" component="input" />
    <button type="submit">Submit</button>
  </form>
)} />

Both libraries offer similar functionality for handling form state and validation in React applications. Formik provides a more structured approach with built-in components, while React Final Form offers greater flexibility and a smaller footprint. The choice between the two often depends on project requirements and developer preferences.

📋 React Hooks for form state management and validation (Web + React Native)

Pros of React Hook Form

  • Smaller bundle size and better performance due to minimal re-renders
  • Easier to set up and use with less boilerplate code
  • Built-in validation with support for Yup, Joi, and Zod schemas

Cons of React Hook Form

  • Less flexibility for complex form scenarios
  • Limited support for dynamic form fields
  • Steeper learning curve for developers familiar with traditional React forms

Code Comparison

React Hook Form:

import { useForm } from "react-hook-form";

const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);

<form onSubmit={handleSubmit(onSubmit)}>
  <input {...register("firstName")} />
  <input type="submit" />
</form>

React Final Form:

import { Form, Field } from "react-final-form";

const onSubmit = values => console.log(values);

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" />
      <button type="submit">Submit</button>
    </form>
  )}
/>

React Hook Form offers a more concise syntax and easier setup, while React Final Form provides more control over form rendering and state management. The choice between the two depends on the specific requirements of your project and personal preferences.

3,625

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

Pros of TanStack Form

  • Built with TypeScript, offering better type safety and developer experience
  • More flexible and customizable, allowing for complex form scenarios
  • Supports both controlled and uncontrolled inputs seamlessly

Cons of TanStack Form

  • Steeper learning curve due to its more complex API
  • Less mature ecosystem compared to React Final Form
  • May be overkill for simple form implementations

Code Comparison

React Final Form:

import { Form, Field } from 'react-final-form'

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" />
    </form>
  )}
/>

TanStack Form:

import { useForm } from '@tanstack/react-form'

const form = useForm({
  defaultValues: { firstName: '' },
  onSubmit: async (values) => {
    // Handle submission
  },
})

<form.Field
  name="firstName"
  children={(field) => <input {...field.getInputProps()} />}
/>

Both libraries offer robust form management solutions, but TanStack Form provides more flexibility and type safety at the cost of increased complexity. React Final Form is simpler to use and has a more established ecosystem, making it a good choice for straightforward form implementations. The choice between the two depends on project requirements and developer preferences.

4,465

Performance-focused API for React forms 🚀

Pros of Unform

  • Built with TypeScript, offering better type safety and developer experience
  • Lightweight and performant, with a smaller bundle size
  • Supports React Native out of the box

Cons of Unform

  • Less mature and less widely adopted compared to React Final Form
  • Fewer integrations and third-party extensions available
  • Documentation is not as extensive or detailed

Code Comparison

React Final Form:

import { Form, Field } from 'react-final-form'

<Form
  onSubmit={onSubmit}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" />
    </form>
  )}
/>

Unform:

import { Form } from '@unform/web'
import Input from './components/Input'

<Form onSubmit={handleSubmit}>
  <Input name="firstName" />
</Form>

Both libraries aim to simplify form management in React applications, but they have different approaches. React Final Form focuses on a more declarative API with render props, while Unform emphasizes a simpler component-based structure. React Final Form has a larger community and more extensive documentation, making it easier for beginners to get started. However, Unform's TypeScript support and lighter weight make it an attractive option for developers who prioritize type safety and performance.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

You build great forms, but do you know HOW users use your forms? Find out with Form Nerd! Professional analytics from the creator of React Final Form.


💰 Wanna get paid the big bucks writing React? Take this quiz and get offers from top tech companies! 💰


🏁 React Final Form

React Final Form

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

✅ Zero dependencies (that affect your bundle size)

✅ Only peer dependencies: React and 🏁 Final Form

✅ Opt-in subscriptions - only update on the state you need!

✅ 💥 3.0k gzipped 💥


React Final Form is sponsored by Sencha.

Comprehensive JS framework and UI components for building enterprise-grade web apps.


💬 Give Feedback on React Final Form 💬

In the interest of making 🏁 React Final Form the best library it can be, we'd love your thoughts and feedback.

Take a quick survey.


React Final Form is a thin React wrapper for Final Form, which is a subscriptions-based form state management library that uses the Observer pattern, so only the components that need updating are re-rendered as the form's state changes.

Getting Started

Philosophy

Examples

API

FAQ

NPM DownloadsLast 30 Days