Hello Coders!
Forms are a crucial part of every application, from signing up, logging in, or collecting a survey from our users. Forms may seem s...
For further actions, you may consider blocking this person and/or reporting abuse
The choice of validation tool sure depends on your choice of UI library. If I want to bootstrap a project with controlled components as fast as possible, then
Formik
is a great choice. However, it really does not scale well with complex form.React Hook Form
was born to solve the performance problem, but well, making it maintainable with external controlled components is a nightmare. God forbids me when I useReact Hook Form
withMUI
, on a form that has ~100 fields or more.It is a trade-off I would say, and apparently, there is no solution in-between that is a jack of all trades.
I realize it is a pain, but you can just create a higher order component for MUI components to make React Hook Form "just work". But, you only have to do it once. Then you can have a
FormTextField
to use instead of just theTextField
.A quick search pulled up this open source code: github.com/dohomi/react-hook-form-mui
There migh be better, honestly I just rolled my own when I was using React Hook Form. But this is a solid example.
Noted also.
🚀🚀
Can you please provide more details why not to use react hook form with MUI as i am thinking to use these two.
Thanks
It's NOT about avoiding the usage
react-hook-form
with MUI. It's more about a complexity of integration when your forms become complex (e.g., 50 fields with conditional choices) should be put into consideration.MUI uses controlled components (e.g.,
TextField
), which means a component has its own internal state (not controlled by the DOM). Indeed,<input>
is used underneath and its properties are exposed viainputProps
. The great thing about Formik is it can directly work with controlled components. Here is an example of Formik with MUI: formik.org/docs/examples/with-mate....react-hook-form
is more complicated to setup in the long run.react-hook-form
by design targets as native and uncontrolled components. It hasController
to help work with external controlled components from libraries like MUI.However,(not a must, see Bill's explaination in the thread below). At that point, you will need to find a hacky way to ensureref
of internalinput
of a component must be exposed somehow, which is not always the casereact-hook-form
a single source of truth.You may ask why don't we break down a form to have smaller chunks. The thing is, some forms have tightly-coupled sections by nature (U.S tax form for example). Breaking it down will result in other set of problems, such as global state control, and can even be more complicated.
Hope this helps!
Ty Hung for your feedback!
With all collected feedback a new improved version will be published at some point.
🚀🚀
Hi there,
Thanks for the feedback. React hook form maintainer here :) I would like to address some of the misconceptions and misunderstanding here.
That's simply not the case, for controlled component react hook form doesn't require users to provide
ref
, the only reason why we are asking forref
is for focus management. We care a lot about accessibility and especially focus on management. So by providing the input ref, we can focus on that errored input if validation fails. However, it's an optional thing to do, but we highly recommend that.For those who are interested are what's Controller, You can watch this video: youtube.com/watch?v=N2UNk_UCVyA which I try to explain how the controller is been built.
Form libraries are generally there to solve a part of your form management, it's not going to be a full solution to solve how you structure your application and break down different parts of your forms. You will still have the responsibility to manage your application and structure them in a meaningful and maintainable way.
As we understand integration with other controlled components is always a challenge due to different component API designs, while the form library has to be as generic as possible. This is something we are constantly seeking to improve. The following codesandbox may help some of you out there (it does live in the doc under
Controller
).codesandbox.io/s/react-hook-form-v...
Again thanks for your feedback, we (the maintainers) will try our best to make the integration a bit easier in the future.
Cheers
Bill
Hello Bill,
Really honored to respond on this thread.
All your feedback is noted for the 2nd version of this article.
Ty for your great work.
🚀🚀
Thanks for correct my misunderstanding, Bill! I will adjust my comment to avoid confusion.
react-hook-form
is really great when working with native and uncontrolled components thanks to its strength in performance and less verbose code. That said, it is also my preference when working with small, controlled forms too.At least in my experience, Formik makes huge forms with controlled components maintainable right out of the gate. I have not figured out how to achieve the same experience with
react-hook-form
though. What I described is more of an edge case, and hopefullyreact-hook-form
can help me handle it efficiently in the future.Thanks again and keep up the great work 🚀
Ty for your new remarks Hung Vu.
🚀🚀
Ty for reading Saquib!
A new (improved) version of the article will be published in the future.
The new content will cover also your Q.
🚀🚀
Ty for your feedback Hung.
Totally agree with you.
Completely agree with this. Especially switching between two projects. One that uses formik and one that uses react hook form.
The performance problem on Formik in complex forms is extremely annoying and gets into hacky territory when trying to fix it.
Even worse than those is when theres a custom built form library when these alternatives already went through all these problems and at least attempted to fix them.
But oh well as is life.
Luckily these two works perfectly well for most of the use cases (e.g., login, profile, product description, etc.). Hopefully there is a library in the future which targets at complex form (like medical report 😩).
Admittedly, building a large form in React is really something due to the way its ecosystem handles global state.
After a while, I ended up stopping use of any of these libs.
For a while, I was using a lib called Hookstate.js hookstate.js.org/
It is a very clever lib. It has great performance in updating child components. I built a set of higher order components to my UI library. I could just pass into a field a
scoped state
as documented here: hookstate.js.org/docs/scoped-state. It looked like this in use:This is great. Hookstate will only update the
StateTextField
on change making huge forms VERY performant. If you ever had to deal with a very large form where an update to one field causes a redraw on all, you'll know what I mean. Unfortunately, PRs and Issues are piling up with Hookstate. Sadly, the maintainers might have less interest in keeping things updated.I'm currently giving Zustand a go github.com/pmndrs/zustand
It is a very small library, less than 1kb.
The goal is to use it as a form validation lib as well as handle my other global state needs.
I am adding a form wrapper like to Zustand like: github.com/Conduct/zustand-forms is what I am looking to do. If you look at the source in this repo, you'll see that it just isn't that much code.
You could make it more complicated by tracking the
ref
andid
of the component and auto matching that with your data object. That code snipped seems ok though, even with a larger or dynamic form.Of course, your own wrapper might be like what I did with Hookstate where I pass it in a state reference and use the component's name or id to map to the state's field name.
So, now I have zustand (which has use-sync-external-store as a dep), zustand-forms (which has immer as a dep). I'm at 4 libs. You could probably refactor not to use immer as well. The point is, if you are like me and use some global state tooling (whatever it is), the form libs become just another item to pile on the stack. The whole solution likely being under 2kb. Of which most of that was going to be in my code anyway.
Hello Brian & ty for you feedback.
Zustand
looks good, I will take a look.I've once used react-hook-form on a pretty big web app project .
At first, I've found it fun but when forms become large, complex and dynamic, we've seen it's limitations, many troubles, bad experience... Then we switched all app to Formik.
And since then, if I need strong and reliable forms I use Formik.
So, my advice is : be careful with react-hook-form !
Thanks for writing!
Size is not a super problem. I'm using
Formik
and probably stick to it for a while.🚀🚀
React Final Form
code looks cleaner.Thanks for sharing!
🚀🚀
Formik also provides hooks if you prefer that pattern
Which one would you recommend for scalable forms where performance is an important factor.
React-Hook-Form ...
Thanks for reading Saamiyah
Hello Luke & Ty for your note.
🚀🚀
Thank you for writing this length and detailed comparison! ❤️
Yw Bill!
🚀🚀
wow ...
ty!
🚀🚀
Really? It came down to having slightly less code and "looks" more intuitive? When you make a comparison article like this, people expect you to have used 2 or more of these libraries for months on end to get a grasp of where each one shines, encountering edge cases, etc.
This is a silly article.
Cool!