Author -  Sai gowtham

How to Reset a File input in React

In this tutorial, we are going to learn about How to Reset a File input in React with the help of an example.

Resetting a File input in React

To reset a file input in React, we can set the event.target.value = null to the input element’s event object. By setting the input element’s target.value property to null resets the file input.

Here is an example:

import React from 'react';

const App = () => {

  const handleFileChange = (event) => {
    // resetting the file input
    event.target.value = null;
  };

  return (
    <div>
      <input type="file" onChange={handleFileChange} />
    </div>
  );
};

export default App;

In the example above, we have added the event.target.value = null to the handleChange function, so whenever a data is changed in the input element it resets the file.

Resetting the file input by Button click

To reset a file input by clicking the button:

  1. Access the input element dom object using the useRef() hook.

  2. Add the inputRef.current.value = null to the button click event handler function, so whenever a user click the reset button resets the input file.

import {useRef} from 'react';

const App = () => {

  // creating the ref for file input
  const inputRef = useRef(null);

  const resetFileInput = () => {
    // resetting the input value
    inputRef.current.value = null;
  };

  return (
    <div>
      <input ref={inputRef} type="file" />
      <button onClick={resetFileInput}>Reset file</button>
    </div>
  );
};

The useRef() hook returns a mutable refernce object of the passed element, whose .current property is initialized to the passed argument as initialValue.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY