r/reactjs • u/salamazmlekom • 2d ago
Discussion Adapter over React hook forms
Those of you that use a form library in your react codebase. Do you tend to create an adapter over it or just use it directly? The reason I am asking is because I want to protect our codebase in case we need to shift to a different library at some point. It’s much easier to update the adapter in this case than it is to adjust the whole codebase (even with agents).
3
Upvotes
10
u/pampuliopampam 2d ago
Opposing opinion from the robot:
For the love of god don’t write a wrapper around one of the most used most loved form libraries out there.
3
u/yeupanhmaj 2d ago
We used to have an adapter for the RHF, but then I found out that the TanStack Form is friendlier to use in our use case,
2
u/LumPiniF 2d ago
the parts that actually carry over are your field components and your zod schema. a swap mostly touches the form-level glue.
1
u/acraswell 2d ago
An adapter around what? The library or the input components? Because I always build wrappers around the input components and it greatly reduces the amount of code or smaller bugs from inconsistent use of the form library. But it has very little to do with changing the forms library later. It does help, it just isn't the primary motivation
1
u/allaboutzaid 1d ago
As someone still learning React and building small sites, I would not add the adapter yet. For my portfolio and a small clothing-brand site, using React Hook Form directly kept things simple, and the field components and Zod schema are the parts I would actually reuse if I ever switched libraries. Your reason makes sense for a bigger codebase that might change libraries, but on a small project the extra layer feels like more to maintain than the swap it is protecting against.
1
u/worldlymatron 1d ago
I would abstract reusable form components rather than the entire library. Building an adapter for something you might never replace feels unnecessary
1
u/anthony-ball 17h ago
I wouldn't wrap the whole library. What's worth owning is your field components and your schema, because that's where most of the code lives anyway. Keep the RHF calls inside those field components plus one small form wrapper, and a future switch touches a handful of files instead of every screen. A full adapter layer tends to end up mirroring the library's API, and you pay for it every day for a migration that may never happen.
1
u/worstallegation177 2d ago
An adapter's one of those things that feels like overkill until the day you actually need to swap libraries, and then you're the office hero. I've done it both ways. Direct usage is fine if you're on a small team and the lib is super stable, but if there's even a whisper of "what if we switch" in your planning meetings, just wrap the damn thing. The trick is keeping the abstraction thin enough that you're not rewriting the library yourself. Wrap the core methods you actually use, don't try to predict every edge case, and you'll save yourself a weekend of find-and-replace misery later.