日本語

Why onChange Does Not Fire When Re-selecting the Same File in React

​​React​TypeScript

When you pick the same file again in an <input type="file">, onChange does not fire because the browser treats the selection as unchanged and does not send a change event. If you clear the selection with e.currentTarget.value = '' at the end of onChange, after receiving the files, onChange fires again the next time you pick the same file. Use this fix when you want to let the user choose the same file again, for example after a failed image upload or after removing a file from a list.

Note

The test environment is Ubuntu 24.04 with Node.js 22.23.4. I used react and react-dom 19.3.1, @types/react 19.3.0, TypeScript 7.0.2, and esbuild 0.28.2. I type-checked the .tsx files with tsc and then built them as a development bundle. The browser was Chromium 153, driven by Playwright 1.63.0. I received the file chooser through Playwright's filechooser and passed files with setFiles, instead of a person picking files on screen. The cat.png and dog.png files I passed were dummy files with short text content. I did not test Firefox, Safari, mobile browsers, or production builds.

onChange does not fire when the same file is selected again

First, I checked an input with no workaround. The component only counts the onChange calls and shows the names of the received files.

tsx
import { useState, type ChangeEvent } from 'react';

export function PlainPicker() {
  const [names, setNames] = useState<string[]>([]);
  const [calls, setCalls] = useState(0);

  function handleChange(e: ChangeEvent<HTMLInputElement>) {
    setCalls((c) => c + 1);
    setNames(Array.from(e.currentTarget.files ?? [], (f) => f.name));
  }

  return (
    <section>
      <input type="file" onChange={handleChange} />
      <p>onChange: {calls}回 / 選んだファイル: {names.join(', ') || 'なし'}</p>
    </section>
  );
}

These are the results after selecting cat.png, then cat.png again, then dog.png.

text
1回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: cat.png」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
2回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: cat.png」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
3回目に dog.png を選んだ後: 「onChange: 2回 / 選んだファイル: dog.png」 input.files.length=1 input.value="C:\\fakepath\\dog.png"

On the second selection of cat.png, the count stayed at 1. When I selected the different file dog.png, onChange fired again.

In the upper "do nothing" panel and the lower "clear value at end of onChange" panel, cat.png is selected twice each. On the second selection, the upper count stays at 1, while the lower count increases to 2
The onChange count when cat.png is selected twice. Top: no workaround. Bottom: clearing the value

The cause: the browser treats the selection as unchanged

As the official <input> reference describes, React's onChange works the same way as the browser's input event. If the browser does not send the event, onChange does not fire. The browser decides whether to send that event.

The HTML specification says that when the file picker closes, it sends input and change if the selection changed, and sends cancel if the selection did not change. However, the spec leaves it to the browser whether re-selecting the same file counts as a change or as a cancellation. A note in the steps for showing the picker says this. The MDN page for <input type="file"> states that re-selecting the same file delivers cancel.

I received cancel with ref and addEventListener, not with a JSX onCancel prop. With @types/react 19.3.0, writing onCancel on an input made tsc report this error:

text
check/OnCancel.tsx(3,29): error TS2322: Type '{ type: "file"; onCancel: () => void; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
  Property 'onCancel' does not exist on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.

This component shows which of change and cancel arrived, in order.

tsx
import { useEffect, useRef, useState } from 'react';

export function CancelWatchPicker() {
  const [log, setLog] = useState<string[]>([]);
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    const input = inputRef.current;
    if (!input) return;
    const onCancel = () => setLog((l) => [...l, 'cancel']);
    input.addEventListener('cancel', onCancel);
    return () => input.removeEventListener('cancel', onCancel);
  }, []);

  return (
    <section>
      <input ref={inputRef} type="file" onChange={() => setLog((l) => [...l, 'change'])} />
      <p>届いたイベント: {log.join(' → ') || 'なし'}</p>
    </section>
  );
}

I selected the files in the same order in Chromium. As MDN describes, on the second selection of cat.png, cancel arrived instead of change.

text
1回目に cat.png を選んだ後: 「届いたイベント: change」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
2回目に cat.png を選んだ後: 「届いたイベント: change → cancel」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
3回目に dog.png を選んだ後: 「届いたイベント: change → cancel → change」 input.files.length=1 input.value="C:\\fakepath\\dog.png"

The fix: clear the value at the end of onChange

The browser treats the selection as unchanged because a selection still remains, so clear the selection once you have received the files. The spec allows only an empty string to be assigned to the value of a file input, and clearing it also empties the list of selected files.

tsx
function handleChange(e: ChangeEvent<HTMLInputElement>) {
  // Copy into an array first, then clear value
  const files = Array.from(e.currentTarget.files ?? []);
  e.currentTarget.value = '';
  setCalls((c) => c + 1);
  setNames(files.map((f) => f.name));
}

When I selected the files in the same order, onChange fired all three times. input.files returned to 0 files each time.

text
1回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: cat.png」 input.files.length=0 input.value=""
2回目に cat.png を選んだ後: 「onChange: 2回 / 選んだファイル: cat.png」 input.files.length=0 input.value=""
3回目に dog.png を選んだ後: 「onChange: 3回 / 選んだファイル: dog.png」 input.files.length=0 input.value=""

Clearing the selection also removes the file name shown by the browser. In the GIF above, the lower field returns to "No file chosen" next to the button. Keep the names of the selected files in state and display them yourself.

If you store the FileList in state, it becomes empty

The reason I used Array.from to copy the files into an array before clearing value is this: when I stored e.currentTarget.files (a FileList) directly in state and then cleared value, the FileList inside the state also became empty.

tsx
function handleChange(e: ChangeEvent<HTMLInputElement>) {
  setFiles(e.currentTarget.files); // Store the FileList as is
  e.currentTarget.value = '';
  setCalls((c) => c + 1);
}
text
1回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: なし」 input.files.length=0 input.value=""
2回目に cat.png を選んだ後: 「onChange: 2回 / 選んだファイル: なし」 input.files.length=0 input.value=""
3回目に dog.png を選んだ後: 「onChange: 3回 / 選んだファイル: なし」 input.files.length=0 input.value=""

onChange fired every time, but no file appeared on screen. Store an array of the File objects copied out of the list.

How other fixes differ

Besides clearing the value inside onChange, I also tested two other approaches. Both made onChange fire when the same file was selected again. The difference is when the selection is cleared.

Clearing the value in onClick, just before the picker opens

tsx
<input
  type="file"
  onClick={(e) => {
    e.currentTarget.value = '';
  }}
  onChange={(e) => {
    setCalls((c) => c + 1);
    setNames(Array.from(e.currentTarget.files ?? [], (f) => f.name));
  }}
/>
text
1回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: cat.png」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
2回目に cat.png を選んだ後: 「onChange: 2回 / 選んだファイル: cat.png」 input.files.length=1 input.value="C:\\fakepath\\cat.png"
3回目に dog.png を選んだ後: 「onChange: 3回 / 選んだファイル: dog.png」 input.files.length=1 input.value="C:\\fakepath\\dog.png"

After selecting, the file stays in input.files, and the browser field still shows the file name. The cost is that the selection is cleared as soon as you click the field to open the picker. The following is the state after selecting cat.png and then clicking the field again, before choosing anything.

text
cat.png を選んだ後: input.files.length=1 「onChange: 1回 / 選んだファイル: cat.png」
もう一度開いた時点(まだ選んでいない): input.files.length=0 「onChange: 1回 / 選んだファイル: cat.png」

The input is already empty, but onChange has not fired yet, so the screen still shows cat.png. In other words, there is a period where the input and the state disagree. With the version that clears the value inside onChange, the input is always empty and the selected file lives only in state, so this mismatch does not occur.

Recreating the input by changing its key

tsx
const [inputKey, setInputKey] = useState(0);

<input
  key={inputKey}
  type="file"
  onChange={(e) => {
    setCalls((c) => c + 1);
    setNames(Array.from(e.currentTarget.files ?? [], (f) => f.name));
    setInputKey((k) => k + 1); // The next render swaps in a new input
  }}
/>
text
1回目に cat.png を選んだ後: 「onChange: 1回 / 選んだファイル: cat.png」 input.files.length=0 input.value=""
2回目に cat.png を選んだ後: 「onChange: 2回 / 選んだファイル: cat.png」 input.files.length=0 input.value=""
3回目に dog.png を選んだ後: 「onChange: 3回 / 選んだファイル: dog.png」 input.files.length=0 input.value=""

The results are the same as clearing inside onChange. As the official documentation section "Resetting state with a key" describes, when the key changes, React treats the new element as a different one from the previous input and recreates it. In my test, the recreated input had no selection, and input.files.length was 0. However, this approach adds one more piece of state for the key. Setting value = '' takes one line and is less work.

In an earlier article, "How to create a drop zone for files in React," I used the same fix on the input behind the "Choose file" button. In "How to select a folder and list the files inside it in React," I also confirmed that clearing value empties a FileList you saved earlier. This article tested why onChange does not fire for the same file, and how the other approaches differ.

​Posted by 86