Maaz Zindani

Loading the index…

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

A React input keeps its old text when defaultValue changes

ErrorInput shows the first record's value after selecting another record

Short 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.

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

Fix it fast

  1. 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=*.tsx
  2. 2Match what you see, then jump to its fix

    1. 1The prop or state changes (you can log it), but the text on screen stays the samedefaultValue is read once, when the input mountsFix
    2. 2Other 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. 3The 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

What does the error look like?

  • Input shows the first record's value after selecting another record
  • A 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 dev

Confirmed by react.dev

Did this fix it?

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=*.tsx

2Fix

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 test

Confirmed by react.dev

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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:

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.