DEV Community

Cover image for React Hook Form vs. Formik: A technical and performance comparison
Brian Neville-O'Neill
Brian Neville-O'Neill

Posted on • Originally published at blog.logrocket.com on

React Hook Form vs. Formik: A technical and performance comparison

Written by Siegfried Grimbeek✏️

Introduction

As JavaScript developers, we are all aware of the complexities and intricacies that we may encounter when working with React and forms. We have all struggled with form verbosity, form validations, and managing the state of the form and its components.

It was these struggles that led to the development of Formik. Formik was first released about two years ago, and it addressed several shortcoming of its “predecessor” Redux Form, which was released four years ago.

Then, about seven months ago, React Hook Form was released, which in turn addressed some of the shortcomings of Formik.

Formik outperformed Redux Form in terms of size, best practices, and performance, but in this article, we will see how Formik measures up against the new kid on the block, React Hook Form. We will compare the two libraries and determine who will emerge as the victor in the end.

LogRocket Free Trial Banner

Technical comparison

Below are the download statistics for React Hook Form and Formik, which clearly shows how new React Hook Form is:

Formik Vs. React Hook Form Download Statistics

But apart from the number of downloads, the above also shows the size, last updates, and the open issues, which are all good metrics on which to judge the libraries.

Based on the minzipped size, React Hook Form comes in at less than half the size of Formik, and it can also be deducted that both libraries are in active development since they get updated at an almost daily rate. One thing to note is the difference in open issues. Here, React Hook Form outperforms Formik, but the open issues will increase for it as grows in popularity.

Next, let’s compare the codebase and the dependencies:

Formik module composition:

Formik Dependencies
Formik has nine dependencies.

React Hook Form module composition:

React Hook Form Dependencies
React Hook Form has no dependencies.

So what does the above mean? The fewer dependencies a library has, the better. Take the infamous “left pad” disaster as an example. The left pad disaster occurred when one developer unpublished his npm modules and broke thousands of other modules that were dependent on it, so yes — fewer dependancies is definitely better.

It is clear that both modules are actively being developed, and both have active chat communities on Spectrum.

So, to summarize:

Formik React Hook Form
Weekly Downloads 533,416 16,797
Size 12.6kB 5.2kB
Open Issues 393 6
Dependencies 9 0
Winner 🥇

With its smaller size and zero dependencies, React Hook Form is the clear winner here.

Performance comparison

Component re-renders is an important factor to consider when implementing any functionality in React. We want to avoid unnecessary re-render cycles as much as possible, as this could lead to major performance issues as an app grows. So let’s see how Formik measures up to React Hook Form:

Formik Re-renders
Total re-renders: 30+

React Hook Form Re-renders
Total re-renders: 3

From this, we can clearly see that in terms or re-rendering, React Hook Form takes first prize.

Another performance concern when developing React applications is the time to mount, which refers to the time it takes React to insert a component into the DOM. Naturally, we aim for the lowest time to mount possible because longer mounting times can cause perceived latencies and delays. Again, let’s square up Formik vs. React Hook Form:

Formik:

Formik Time To Mount

  • No. of mounts: 6
  • No. of committing changes: 1
  • Total time: 2070ms

React Hook Form:

React Hook Form Time To Mount

  • No. of mounts: 1
  • No. of committing changes: 1
  • Total time: 1800ms

The above tests are based on a very simple form, so increasing the complexities would also cause the difference in time to mount to increase, but it is clear that React Hook Form outperforms Formik. In summary:

Formik React Hook Form
Re-renders 30+ 316,797
No. of mounts 6 1
No. of comitting changes 1 1
Total mounting time 2070ms 1800ms
Winner 🥇

With its fewer re-renders and quicker time to mount, React Hook Form is the clear winner.

The tests are from the React Hook Form website, and the code and text explanations can be found there. I did run a performance test independently on my own machine and got similar findings.

Development comparison

To evaluate the subtle differences and the caveats of each library, we are going to build a form with several different input types and validations:

Field Name Field Type Field Validation Required
Username Text Max length = 20
Name Text Max length = 50
Email Email Valid Email (Pattern)
Mobile Number Tel Max length = 12
Website URL None
Password Password Uppercase, lowercase, number/special char, and min. 8 chars
Gender Radio None
Date of Birth Date MM/DD/YYYY
About Textarea None
Subscribe to Newsletter Checkbox None

I added Bootstrap to the form for aesthetics, but also to demonstrate how easily it is integrated into each respective module. The submit event will log the form data to the console.

I did not include any additional libraries for validations or assisting with the state management; we will rely purely on the functionality of each library.

React Hook Form

As I started with developing the form, I discovered the React Hook Form form builder:

React Hook Form Form Builder

This proved to be a game changer, as it allows users to very easily create the form fields and their respective validations.

Note that he form builder is not a one-size-fits-all solution, but it does allow us to quickly bootstrap a form with generic HTML5 input fields. I needed to adjust a few minor things, especially when applying the Bootstrap elements and classes, but it did still save a lot of time.

Below is the CodeSandbox for the React Hook Form form:

I found the development to be really simple, and the great thing about React Hook Form is that it allows you to plug it into basically any framework or UI library.

In the this example, we are using React Hook Form with a standard HTML5 form, inputs, and validation patterns. The error message integration also proved to be quick, simple, and easy to implement.

Below is an example of a form input, validation, and error messages:

<div class="form-group">
  <input
    class="form-control"
    type="text"
    placeholder="Username"
    name="Username"
    ref={register({ required: true, maxLength: 20 })}
  />
  {errors.Username && errors.Username.type === "required" && errorMessage(required)}
  {errors.Username && errors.Username.type === "maxLength" && errorMessage(maxLength)}
</div>
Enter fullscreen mode Exit fullscreen mode

Overall, I found React Hook Form to be a very developer friendly experience. I enjoy how light, clear, and concise the code is!

Formik

I could not find anything similar to the React Hook Form form builder for Formik but I was able to repurpose a lot of the code and use it to build the form, below is the Formik CodeSandbox example:

Just like React Hook Form, Formik also proved to be an excellent development tool and was very simple to implement.

Below is an example of a form input, validation, and error messages:

<div className="form-group">
    <Field
      className="form-control"
      type="text"
      placeholder="Username"
      name="username"
      validate={validateUserName}
    />
    {errors.username && touched.username && errorMessage(errors.username)}
</div>
Enter fullscreen mode Exit fullscreen mode

It is implemented in a very similar way to React Hook Form, but notice that Formik makes use of the <Field/> component, unlike React Hook Form, which can be used with just HTML5 input elements.

Validation with Formik also needs to be explicitly developed and applied to each input:

const validateUserName = value => {
  let error;
  if (!value) {
    error = required;
  } else if (value.length > 12) {
    error = maxLength;
  }
  return error;
};
Enter fullscreen mode Exit fullscreen mode

Conclusion

I found React Hook Form very easy to use, and it comes with a very small footprint since error handling is integrated and no initial values need to be set.

Formik was also simple to use and has a small footprint but does not beat React Hook Form. Additionally, I had to deep-dive into the documents and Google some features because they were not obvious or clearly documented — for example, the textarea, which is displayed as follows:

<Field component="textarea" />
Enter fullscreen mode Exit fullscreen mode

And the winner is:

Formik React Hook Form
Winner 🥇

Additional functionality and features

Formik React Hook Form
React Native
TypeScript
Nested components
Class components
Code examples
Clear documentation
YUP integration
Redux integration

Conclusion

It is clear that React Hook Form is the overall winner. Although it is still new, it is already an incredibly powerful library.

On the homepage, it states: “*Reducing the amount of code that you have to write is one of the primary goals for React Hook Form,” and it definitely succeeds at this.

I must admit that I am a very big fan of React Hooks and the simplicity they bring to a solution. Therefore, I may be biased to React Hook Form, but the performance and size statistics speak for themselves, and here, too, React Hook Form is the winner.

This by no means makes Formik a bad solution, and if you need a form solution that is still compatible with Class Components, Formik is the way to go since React Hook Form only supports functional components.


Editor's note: Seeing something wrong with this post? You can find the correct version here.

Plug: LogRocket, a DVR for web apps

 
LogRocket Dashboard Free Trial Banner
 
LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.
 
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.
 
Try it for free.


The post React Hook Form vs. Formik: A technical and performance comparison appeared first on LogRocket Blog.

Oldest comments (12)

Collapse
 
theodesp profile image
Theofanis Despoudis

Every now and then, a new React Form Library emerges that supposed to be the "best".
Before it was Redux-Forms, now is Formik, next is.... you get the point!
Only time will tell.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
genta profile image
Fabio Russo

Inability to work with Form? I'm using React, since years, and I always work with Forms by myself...

Collapse
 
spoeken profile image
Mathias Madsen Stav • Edited

So, did you test Formik 2 or the old one. Formik has had a rc out for a long time that was just released. Also for the performance tests, did you use fast field or just the field component? Would be nice to see the actual code you set up for the render tests as well, to know exactly how it was tested :) It seems you have the default validateOnChange setting, which is true.

Collapse
 
qmixi profile image
Piotr Kumorek • Edited

Hey! Really cool article and definitely I'm going to play with the React Hook Form library :)

One notice - in the table with summary for performance comparison in cell representing re-renders for React Hook Form probably should be 3 instead of 316,797.

Collapse
 
bluebill1049 profile image
Bill

thanks, Piotr, feedback welcome after you trying :)

Collapse
 
bluebill1049 profile image
Bill • Edited

hey Justin, author of react hook form here, wondering how can we improve the experience here? let me know your feedback. In case you didn't see this section: react-hook-form.com/advanced-usage...

cheers
bill

 
bluebill1049 profile image
Bill

Thanks for the feedback! I think I do lack a good example of doing Field Array, which I should :). The lib itself doesn't expose any of those utility functions which you have mentioned and it's my purpose, I would prefer users to compose and work off their own logic and keep the API primitive. To work with react-hook-form with Field array, you need to maintain your own state to generate the fields and use the 'name' attribute as your name of the data source. I will be working on a good example and upload the website soon.

 
bluebill1049 profile image
Bill

Here is the example which i have built: codesandbox.io/s/react-hook-form-f... let me know what do you think :)

 
bluebill1049 profile image
Bill

Hey Justin,

Thanks very much for your feedback, which I am 100% agree :) I think there are few things which I was try to highlight in the demo:

  1. Performance: skip re-render onChange which trigger render at the root of your app (not sure if you notice the render counter on the top right)
  2. Primitive API: let you compose and work your way through (be the driver)

but this is not the end of it, I am going to upload the example to the website and when I get some more free time, I can work on an abstract version of Field Array, which basically cooks that logic into the small component RHFArrayField. I will get to it, just matter of time :)

again much appreciated your feedback and time to looking into it.

cheer
bill

 
bluebill1049 profile image
Bill • Edited

Hey Justin,

as promised :) we have a new custom hook for Field Array.

react-hook-form.com/api#useFieldArray

example below:

import React from "react";
import { useForm, useFieldArray } from "react-hook-form";

function App() {
  const { register, control, handleSubmit } = useForm({
    // defaultValues: {}; you can populate the fields by this attribute 
  });
  const { fields, append, prepend, remove } = useFieldArray({ control, name: "test" });

  return (
    <form onSubmit={handleSubmit(data => console.log("data", data))}>
      <ul>
        {fields.map((item, index) => (
          <li key={item.id}>
            <input name={`test[${index}].name`} ref={register} /> 
            <button onClick={() => remove(index)}>Delete</button>
          </li>
        )}
      </ul>
      <section>
        <button type="button" onClick={() => append({ name: "test" })} >
          append
        </button>
        <button type="button" onClick={() => prepend({ name: "test1" })}>
          prepend
        </button>
    </form>
  );
}
 
bluebill1049 profile image
Bill

This is a bug. Thanks for reporting the issue. I have resolved the problem. let me know if there is still an issue.

cheers
bill