A React input keeps its old text when defaultValue changes
Input shows the first record's value after selecting another recordShort answer
defaultValue only sets the starting text. React hands the input to the browser after the first render, so a later change to the prop does nothing. Give the input a key that changes with the record, or make it controlled with value and onChange. Do not copy props into state inside an effect.
Fix it fast
1Find out which cause you have
Look at the input in the component. Does it use defaultValue, value, or both?
grep -rnE "defaultValue|value=\{" src --include=*.jsx --include=*.tsx2Match what you see, then jump to its fix
- 1
The prop or state changes (you can log it), but the text on screen stays the samedefaultValue is read once, when the input mountsFix - 2
Other code (a reset button, a fetch that fills the form) must change the text from outside the inputThe field should follow state at every renderFix - 3
The old value flashes for one frame before the new one appears, or the form needs the reset code copied into several componentsState is reset with an effect that runs one render too lateFix
- 1
What does the error look like?
Input shows the first record's value after selecting another recordA component is changing an uncontrolled input to be controlled.
Cause 1 of 3
defaultValue is read once, when the input mounts
1Check
The React docs describe defaultValue as the initial value. The input is uncontrolled: the browser owns what it shows, and React does not push new defaults into it.
function Field({ name }) {
console.log("render with", name); // logs the new value
return <input defaultValue={name} />; // screen does not change
}2Fix
When a different record should show a fresh input, give the input a key that identifies the record. React then removes the old input and mounts a new one that reads the new defaultValue.
function ProfileForm({ userId, name }) {
return <input key={userId} name="name" defaultValue={name} />;
}3Verify
Switch records. The input shows the new value. Unsaved typing in the old input is discarded, which is the point.
npm run devConfirmed by react.dev
Cause 2 of 3
The field should follow state at every render
1Check
Only a controlled input follows what you pass on every render. It needs value and onChange together.
grep -rn "onChange" src --include=*.tsx2Fix
Hold the text in state and pass it as value, with an onChange that updates it. Start the state as an empty string, not undefined or null.
const [firstName, setFirstName] = useState('');
return (
<input
value={firstName}
onChange={e => setFirstName(e.target.value)}
/>
);3Verify
Call setFirstName from a button. The input text changes at once.
npm testConfirmed by react.dev
Cause 3 of 3
State is reset with an effect that runs one render too late
1Check
A useEffect that calls setState when a prop changes renders once with the stale state first. The React docs recommend a key on the component instead.
grep -rn "useEffect" src | grep -i "set"2Fix
Pass the prop as a key to the component that owns the state. React resets that state, and the state of its children, on its own.
export default function ProfilePage({ userId }) {
return <Profile userId={userId} key={userId} />;
}3Verify
Change userId and watch that the form resets on the first paint.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Pick one mode per input: defaultValue for uncontrolled, value plus onChange for controlled. Never switch between them.
- Initialize controlled state to an empty string, or write value={x ?? ''} for data that loads late.
- Key forms by the id of the record they edit.
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