How React’s useRef Hook Can Help You Reset an Input Field After a Successful Upload

Using React, you may find it more awkward to reset some input fields than others. In particular, file inputs can be problematic, yet these are the exact fields you’ll want to reset after a successful file upload.

Thankfully, the useRef hook provides a simple solution. Learn how you can clear a file input field with the useRef hook after a successful upload.

4

Creating a Simple Upload Form

To demonstrate how to reset a file input field with useRef, you’llcreate a simple React formwith an input field that accepts an image.

First, set up a state value named selectedFile using the useState hook to track the selected file. The initial state for selectedFile will be null since the user has not selected a file yet.

A memory card near a laptop

Also, create a handler function called handleFileChange that updates the selectedFile state when a user selects a file. Add a second function called handleSubmit, which should upload the state when a user uploads the file.

When the file upload is complete, the application should clear the input field, which you’ll learn how to do below.

Laptop screen with initial react code and logo

Clear the Input Field After a File Upload

If this was a text field, you could clear the input by setting the state to an empty string:

But this won’t work with an input field of typefile. Setting the selectedField state variable to an empty string only removes the file data from the state and not from the DOM. This is because this state does not reference the input value.

netflix logo with popcorn and film board

To clear the input value, you must create a reference to the file input using the useRef hook. In this example, import useRef from React and create a ref object called fileRef:

Then reference the ref in the input field as shown below.

lm studio openai gpt-oss-20b local ai on comptuer screen.

React sets thecurrentproperty of the ref variable to the DOM element which means you can get the file value like this:

you may then reset this value by assigning null to it.

Encapsulate this in a function named handleReset like this:

Then call this function when you successfully upload a file to clear the input field.

Why You Should Reset Input Fields After File Uploads

It’s generally a good practice to reset the input field after a successful file upload. This is because it gives the user a clear indication that their upload was successful and also provides them with an opportunity to upload another file without having to manually clear the input field.

Hooks are a vital part of your React toolbox. Move beyond the basics with two hooks that provide additional functionality.

Make sure you don’t miss these movies and shows before Netflix removes them.

You don’t need to fork out for expensive hardware to run an AI on your PC.

Obsidian finally feels complete.

Sometimes the smallest cleaning habit makes the biggest mess.

The fix was buried in one tiny toggle.

Technology Explained

PC & Mobile