Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React throws when a file input is given a value

ErrorFailed to set the 'value' property on 'HTMLInputElement'

Short answer

A file input cannot be controlled. Browsers do not let a script set its value, so passing a value prop or assigning input.value throws. Leave the input uncontrolled, read the chosen files from event.target.files in onChange, and keep the File objects in state instead of the input value.

  • Applies to React 19, Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every input of type file and see whether it receives a value prop or a value assignment.

    grep -rn "type=\"file\"\|type='file'" app components src 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The component renders <input type="file" value={something} />.A value prop is passed to type="file"Fix
    2. 2A ref, a form reset helper or a library assigns a string to the element's value.Code sets input.value on the file elementFix

What does the error look like?

  • Failed to set the 'value' property on 'HTMLInputElement'
  • How to fix Failed to set the 'value' property on 'HTMLInputElement' for react?

Cause 1 of 2

A value prop is passed to type="file"

1Check

Passing value to an input makes it controlled, so React forces that value onto the element. A file picker's value cannot be set that way.

grep -rn -B1 -A3 "type=\"file\"" app components

2Fix

Remove value. Read the selection in onChange and store the File objects.

'use client'
import { useState } from 'react'

export default function Upload() {
  const [file, setFile] = useState<File | null>(null)
  return (
    <input
      type="file"
      onChange={(e) => setFile(e.target.files?.[0] ?? null)}
    />
  )
}

3Verify

Pick a file. The error is gone and the state holds a File object.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

Code sets input.value on the file element

1Check

The file picker's value is the file name prefixed with C:\fakepath\, and a script cannot set it. Search for assignments.

grep -rn "\.value *=" app components src 2>/dev/null

2Fix

Do not write to the input. Use the files property to read what was chosen, and show names or previews from the File objects.

const input = e.currentTarget
const files = input.files
if (files && files.length > 0) {
  for (const file of files) {
    console.log(file.name, file.size, file.type)
  }
}

3Verify

Select one and then several files and confirm each File is listed.

npm run dev

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Treat file inputs as uncontrolled and keep the chosen File objects in your own state.
  • Give the input a name and read it through FormData when you submit a form.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.