DEV Community

A Better Guide To Forms in React

Andrew Jones on October 11, 2023

The React ecosystem is lush with libraries, articles, videos, and more resources for every web development topic you could imagine. However, over t...
Collapse
 
jdgamble555 profile image
Jonathan Gamble

Or even better, use TypeScript, and use Object.fromEntries:

const handleSubmit = (event: FormEvent<YourType>) => {
  const formData = new FormData(e.target)
  const { name, email, address } = Object.fromEntries(formData);
  ...
};
Enter fullscreen mode Exit fullscreen mode

J

Collapse
 
ajones_codes profile image
Andrew Jones

Great addition! Thank you! 🙏

Collapse
 
copleykj profile image
Kelly Copley

When using React, I've not found a use case yet that React Hook Form didn't satisfy and make my life 1000x simpler

Collapse
 
trenaryja profile image
Justin Trenary

You've swayed me away from using controlled fields with state for form data, completely agreed with your rationale for using uncontrolled inputs where possible:

  • avoiding boilerplate-y code
  • favoring the more "web-idiomatic" approach
  • better performance through shipping less js with server-side rendering

This is my first time hearing of the FormData API, I'll definitely be using that going forward!

Would love to read a follow up where you discuss more about how React-Hook-Form fits into the picture. Great article dude, thanks for taking the time to write it

Collapse
 
rawdyn profile image
Rawdyn Nutting

I nearly didn't read this article thinking I knew enough about forms. 😏

I'm very glad I did. Really great perspective and will definitely make me revisit how I implement forms in future projects.

More broadly, really well written article. 👍
Perfect balance of being concise and providing detail.

Collapse
 
manuqs33 profile image
manuqs33 • Edited

Excellent article, it helped me a lot. In my job we created dozens of controlled forms, and it was cumbersome and hard at moments, with a lot of bug potential. I didn't use uncontrolled because I didn't know about new FormData(). The way you manage uncontrolled forms is definitely the way, managing controlled inputs only when needed

Collapse
 
voko profile image
Ko • Edited

I use class components because instances of such components exist. Each form consists of forms, forms of forms, ... and so on. Each form has a public method get() {return {dataA: refs.formA.get(), dataB: refs.formB.get(), dataC: ":)"}} that will return form data that was collected from child forms by calling get().

This simple solution allows me to create very large and complex shapes. Unfortunately, this approach is not possible on functional components

Collapse
 
joshuarost profile image
Joshua

I really like this approach. The question is, how do you handle more complex forms with nested data in arrays? Something like:
{ name: "James", hobbies: [{name: "football"...}, {name....}]}

Collapse
 
andresdavi1 profile image
Andrés Da Viá

Besides FormData we can also make use of the form submit event and access input elements like => event.target.elements.[inputID] or event.target.elements.[inputName]

Image description

Collapse
 
tomekponiat profile image
Tomek Poniatowicz

And if you want to send form data to your email without touching the server code you can do it with mailik.dev/

Collapse
 
roohialik profile image
Roohi Ali

It was quite useful, thank you

Collapse
 
eduardojm profile image
Eduardo Oliveira

This looks good and i definitely will try to use this approach to reduce complexity on my side projects.

Collapse
 
mpushparaja13 profile image
Pushparaja

Nice article . great info, Thanks for sharing

Collapse
 
skreddysasi profile image
S K Reddy Sasi

Thanks for sharing 🙌

Collapse
 
lebbe profile image
Lars-Erik Bruce

Very good writeup! 😎