π Mastering Forms in React: From Basics to File Uploads and Beyond
Working with forms in React might seem intimidating at first, but itβs actually quite simple once you understand how React works with the DOM and standard HTML form behaviors. In this blog post, weβll explore everything from basic form handling to preventing full-page refreshes and uploading files β all using modern React.
Forms have been a part of the web since the very beginning. Originally, submitting a form would reload the entire page and send data via the URL. Thatβs not ideal for modern web applications, which strive to be dynamic and responsive without page reloads.
Thankfully, with React and some JavaScript, we can handle form submissions in a much more user-friendly way.
Hereβs a simple form with a text input:
<formonSubmit={(event) => {event.preventDefault() // Prevent default full-page reloadconst formData = new FormData(event.currentTarget)// Do something with formData}}><label>Username<input name="username" type="text" /></label><button type="submit">Submit</button></form>
Key takeaways:
onSubmit handles the form submission.event.preventDefault() stops the browser from doing a full page reload.event.currentTarget refers to the <form> element.FormData lets you easily grab the formβs input values.Accessibility matters! Always label your inputs clearly:
<label>Name<input name="name" type="text" /></label>
htmlFor and id<label htmlFor="nameInput">Name</label><input id="nameInput" name="name" type="text" />
In React, we use
htmlFor(instead offor) becauseforis a reserved keyword in JavaScript.
Modern HTML supports a variety of input types that simplify validation and UX. Here are a few examples we can add:
<input type="password" name="password" /><input type="file" name="photo" /><input type="color" name="favoriteColor" /><input type="date" name="startDate" />
Explore all standard input types here.
By default, submitting a form triggers a GET request and updates the URL β not great for sensitive data like passwords. To improve:
POST Instead of GET:<form action="/api/onboarding" method="POST">{/* fields */}</form>
File inputs require a different encoding to send file data properly.
If you use default form settings, file inputs only send filenames β not the actual file content.
Use enctype="multipart/form-data":
<formaction="/api/onboarding"method="POST"encType="multipart/form-data">{/* inputs */}</form>
This tells the browser to send form data (including files) as multipart/form-data.
To prevent the full-page reload and handle the form submission manually:
<formaction="/api/onboarding"method="POST"encType="multipart/form-data"onSubmit={(event) => {event.preventDefault()const formData = new FormData(event.currentTarget)console.log(Object.fromEntries(formData))}}>{/* inputs */}</form>
Object.fromEntries(formData) converts the FormData into a plain object so you can easily inspect it in your dev tools.
action Prop (Advanced)React supports an additional, very cool feature: passing a function to the action prop instead of a URL.
<form action={(formData) => {console.log(Object.fromEntries(formData))}}>{/* inputs */}</form>
This is not standard HTML behavior β this is a React-only feature that gives you even more control without needing onSubmit.
Forms are an essential part of web applications, and React makes working with them straightforward β once you understand how to work with the DOM and handle things like file uploads, preventing default behavior, and logging data correctly.
Whether youβre building a login form or an onboarding experience with file uploads, the core principles are the same:
Happy form building! πͺ