Convert Figma logo to code with AI

unform logounform

Performance-focused API for React forms 🚀

4,460
485
4,460
73

Top Related Projects

33,863

Build forms in React, without the tears 😭

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

🏁 High performance subscription-based form state management for React

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

3,724

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

Quick Overview

Unform is a performance-focused React library for creating powerful and flexible forms. It provides a set of components and hooks that simplify form management, validation, and submission while maintaining high performance and extensibility.

Pros

  • Highly performant, with minimal re-renders
  • Flexible and extensible, supporting custom input components
  • Easy integration with popular UI libraries like Material-UI and Chakra UI
  • Strong TypeScript support for type-safe form development

Cons

  • Steeper learning curve compared to simpler form libraries
  • Less opinionated, which may require more setup for complex forms
  • Documentation could be more comprehensive for advanced use cases
  • Smaller community compared to some other popular form libraries

Code Examples

  1. Basic form setup:
import React from 'react';
import { Form } from '@unform/web';
import Input from './components/Input';

function MyForm() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <Input name="email" />
      <Input name="password" type="password" />
      <button type="submit">Submit</button>
    </Form>
  );
}
  1. Using form reference for manual actions:
import React, { useRef } from 'react';
import { Form } from '@unform/web';
import Input from './components/Input';

function MyForm() {
  const formRef = useRef(null);

  function handleSubmit(data) {
    console.log(data);
    formRef.current.reset();
  }

  return (
    <Form ref={formRef} onSubmit={handleSubmit}>
      <Input name="name" />
      <button type="submit">Submit</button>
    </Form>
  );
}
  1. Form validation with Yup:
import React, { useRef } from 'react';
import { Form } from '@unform/web';
import Input from './components/Input';
import * as Yup from 'yup';

function MyForm() {
  const formRef = useRef(null);

  async function handleSubmit(data, { reset }) {
    try {
      const schema = Yup.object().shape({
        email: Yup.string().email().required(),
        password: Yup.string().min(6).required(),
      });

      await schema.validate(data, { abortEarly: false });
      console.log(data);
      reset();
    } catch (err) {
      if (err instanceof Yup.ValidationError) {
        const errorMessages = {};

        err.inner.forEach(error => {
          errorMessages[error.path] = error.message;
        });

        formRef.current.setErrors(errorMessages);
      }
    }
  }

  return (
    <Form ref={formRef} onSubmit={handleSubmit}>
      <Input name="email" />
      <Input name="password" type="password" />
      <button type="submit">Submit</button>
    </Form>
  );
}

Getting Started

To start using Unform in your React project:

  1. Install the necessary packages:
npm install @unform/core @unform/web
  1. Create a basic form component:
import React from 'react';
import { Form } from '@unform/web';
import Input from './components/Input';

function MyForm() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <Input name="name" />
      <Input name="email" />
      <button type="submit">Submit</button>
    </Form>
  );
}

export default MyForm;
  1. Implement a custom Input component:
import React, { useEffect, useRef } from 'react';
import { useField } from '@unform/core';

function Input({ name, ...rest }) {
  const inputRef = useRef(null);
  const { fieldName, register

Competitor Comparisons

33,863

Build forms in React, without the tears 😭

Pros of Formik

  • Larger community and ecosystem, with more third-party integrations and resources
  • Built-in support for Yup schema validation
  • More comprehensive documentation and examples

Cons of Formik

  • Steeper learning curve, especially for complex forms
  • More verbose syntax for form setup and configuration
  • Larger bundle size compared to Unform

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>

Unform:

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

<Form onSubmit={handleSubmit}>
  <Input name="name" />
  <button type="submit">Submit</button>
</Form>

Both Formik and Unform are popular form libraries for React applications. Formik offers a more mature ecosystem and extensive documentation, making it a solid choice for complex form scenarios. However, it comes with a steeper learning curve and more verbose syntax. Unform, on the other hand, provides a simpler API and smaller bundle size, which can be advantageous for smaller projects or when performance is a primary concern. The choice between the two largely depends on the specific requirements of your project and personal preferences.

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

Pros of react-hook-form

  • Smaller bundle size and better performance
  • More extensive documentation and community support
  • Built-in TypeScript support

Cons of react-hook-form

  • Steeper learning curve for complex forms
  • Less flexibility in form structure compared to Unform

Code Comparison

Unform:

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

function MyForm() {
  function handleSubmit(data) {
    console.log(data);
  }

  return (
    <Form onSubmit={handleSubmit}>
      <Input name="email" />
      <button type="submit">Submit</button>
    </Form>
  );
}

react-hook-form:

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

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

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("email")} />
      <button type="submit">Submit</button>
    </form>
  );
}

Both libraries aim to simplify form handling in React applications, but they differ in their approach. react-hook-form focuses on performance and minimal re-renders, while Unform provides a more flexible structure for complex forms. The choice between them depends on the specific needs of your project, such as form complexity, performance requirements, and developer preferences.

🏁 High performance subscription-based form state management for React

Pros of React Final Form

  • More mature and widely adopted in the React community
  • Extensive documentation and larger ecosystem of resources
  • Field-level validation and error handling out of the box

Cons of React Final Form

  • Steeper learning curve due to more complex API
  • Requires more boilerplate code for basic form setups
  • Less TypeScript-friendly compared to Unform

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" placeholder="First Name" />
      <button type="submit">Submit</button>
    </form>
  )}
/>

Unform:

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

<Form onSubmit={handleSubmit}>
  <Input name="firstName" placeholder="First Name" />
  <button type="submit">Submit</button>
</Form>

Both libraries offer powerful form management solutions for React applications. React Final Form provides a more feature-rich and battle-tested option, while Unform offers a simpler API and better TypeScript integration. The choice between the two depends on project requirements, team expertise, and preference for API style.

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
  • Robust validation and error handling capabilities

Cons of redux-form

  • Requires Redux as a dependency, which may be overkill for simpler applications
  • Can lead to increased boilerplate code
  • Performance issues with large forms due to frequent re-renders

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);

unform:

import { Form, Input } from '@unform/web';

const MyForm = () => (
  <Form onSubmit={handleSubmit}>
    <Input name="username" />
    <button type="submit">Submit</button>
  </Form>
);

export default MyForm;

Key Differences

  • unform is more lightweight and doesn't require Redux
  • redux-form provides more out-of-the-box features for complex form scenarios
  • unform offers a simpler API and less boilerplate code
  • redux-form has better TypeScript support and type definitions
  • unform focuses on performance and minimizes unnecessary re-renders
3,724

🤖 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

  • More comprehensive and feature-rich, offering advanced form management capabilities
  • Better TypeScript support and type inference
  • Larger community and more frequent updates

Cons of TanStack Form

  • Steeper learning curve due to its complexity
  • Potentially heavier bundle size for simpler form use cases

Code Comparison

Unform:

import { Form } from '@unform/web';

const MyForm = () => (
  <Form onSubmit={handleSubmit}>
    <Input name="email" />
    <Input name="password" type="password" />
    <button type="submit">Submit</button>
  </Form>
);

TanStack Form:

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

const MyForm = () => {
  const form = useForm({
    defaultValues: { email: '', password: '' },
    onSubmit: handleSubmit,
  });
  return (
    <form.Provider>
      <form.Field name="email" />
      <form.Field name="password" type="password" />
      <button type="submit">Submit</button>
    </form.Provider>
  );
};

Both libraries aim to simplify form management in React applications, but TanStack Form offers more advanced features and better TypeScript support. Unform provides a simpler API and may be easier to get started with for basic form needs. The choice between them depends on the complexity of your form requirements and your familiarity with form libraries.

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

Unform

Easy peasy highly scalable ReactJS & React Native forms! 🚀

npm Coverage Status

Overview

Unform is a performance-focused API for creating powerful forms experiences for both React and React Native. Using hooks, you can build lightweight and composable forms based on ultra-extensible components. Integrate with any form library, validate your fields, and have your data out of the box.

Want to test Unform before using it?

ps: not available with React Native Web or Expo Web, use the iOS/Android devices in Expo Snack.

Need help?

We’re using GitHub Discussions to create conversations around Unform. It is a place for our community to connect with each other around ideas, questions, issues, and suggestions.

Roadmap

If Unform currently doesn't have a certain feature you think it's awesome, be sure to check out the roadmap to see if this is already planned for the future. Otherwise, we recommend you create a discussion describing your enhancement request.

Contributing

Thank you for being interested in making this package better. We encourage everyone to help improve this project with new features, bug fixes, or performance improvements. Please take a little bit of your time to read our guide to make this process faster and easier.

Contribution Guidelines

To understand how to submit an issue, commit and create pull requests, check our Contribution Guidelines.

Code of Conduct

We expect you to follow our Code of Conduct. You can read it to understand what kind of behavior will and will not be tolerated.

License

MIT © Rocketseat

NPM DownloadsLast 30 Days