React throws when a file input is given a value
Failed 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.
Fix it fast
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/null2Match what you see, then jump to its fix
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 components2Fix
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 devConfirmed by react.dev
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/null2Fix
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 devConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb 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 fixWeb 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