How to Reset a File Input in React.js ? Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To reset a file input in React JS, we can use the useRef Hook to create a reference to the input element and modify its properties using the DOM API.Prerequisites:NPM & Node JSReact JSReact useRef HookApproach:To reset a file input in react we will create a reset button and handleReset function. The function will access the input element using useRef Hook and reset the input value to an empty string.The useRef is a React Hook that stores mutable values persistently without triggering a re-render. It can hold references to DOM nodes, counters, or timers.Syntax: inputFile.current.value = "";Steps to Create React Application:Step 1: Create a react application by using this commandnpx create-react-app file-inputStep 2: After creating your project folder, i.e. file-input, use the following command to navigate to it:cd file-inputProject Structure:Example: This example uses the useRef hook to create reference and reset the corresponding file input. JavaScript // Filename - App.js import React, { useRef } from "react"; export default function App() { // Style object for the container div const container = { margin: "5rem", display: "flex", flexDirection: "column", alignItems: "center", }; // Style object for the input element const input = { width: "400px", padding: "8px", border: "1px solid #ccc", borderRadius: "4px", fontSize: "14px", boxShadow: "0px 0px 10px 0px grey", }; // Style object for the button element const button = { marginTop: "1rem", padding: "10px 10px 10px 10px", border: "none", borderRadius: "8px", backgroundColor: "green", color: "#fff", fontSize: "1rem", cursor: "pointer", transition: "all linear 0.5s", boxShadow: " 0px 0px 10px 0px grey", }; // Ref object to reference the input element const inputFile = useRef(null); // Function to reset the input element const handleReset = () => { if (inputFile.current) { inputFile.current.value = ""; inputFile.current.type = "text"; inputFile.current.type = "file"; } }; // Render function to display the input // element and the reset button return ( <div style={container}> <input style={input} type="file" ref={inputFile} /> <button style={button} onClick={handleReset}> Reset Input </button> </div> ); } Step to Run the Application: Open the Terminal and enter the command listed below. npm startOutput: The output will be visible on the http://localhost:3000/ on the browser window. Comment More infoAdvertise with us Next Article How To Download PDF file in ReactJS? S saurabhkumarsharma05 Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to add code input in React JS? In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our 2 min read How to Read CSV files in React.js ? CSV (Comma-Separated Values) files are a common format for storing and exchanging tabular data. When working with React.js, reading and parsing CSV files can be a useful task for handling data input. To read CSV files in React JS we have to use external libraries as there in no inbuilt methods avail 4 min read How to create refs in React JS? React JS, a powerful and declarative JavaScript library for building user interfaces, provides a feature known as "refs" that allows developers to interact with the underlying DOM elements directly. Refs are generally used for the following purposes:Managing focus, text selection, or media playback. 4 min read How to refresh a file in Node.js ? Node.js has seen an important growth in past years and is still increasing its value in many organizations and business models. Companies like Walmart or PayPal have already started to adopt it. NPM, the package manager of Node.js has been already installed when you install Node.js and is ready to r 2 min read How To Download PDF file in ReactJS? Downloading files, such as PDFs, in a React application can be easily achieved using two main methods: Using the HTML DOM Anchor Object and using the fetch() Method. Both of these approaches allow you to download files and store them locally, but they differ in flexibility and complexity. Let's expl 4 min read How to Change an Uncontrolled Input in React? Uncontrolled input refers to form elements such as text fields, checkboxes, or radio buttons, where the value isn't directly managed by the React state. Their values are stored within the DOM itself and are independent of the React state. It can be challenging to change their input. In this article, 2 min read Like