Maaz Zindani

Loading the index…

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

A React range slider that will not move or does not update its value

ErrorYou provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.

Short answer

A React range slider that will not move is almost always a controlled input: you passed value but no onChange, so React keeps resetting the thumb. Other causes are state that starts undefined, a value that arrives as a string, and min, max or step defaults of 0, 100 and 1 that clamp your number. Open the console first, React names the problem.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser console and drag the slider. If React prints the read-only warning, go to the first cause. If the console is quiet and the thumb jumps to an end or the numbers look wrong, look at the last two causes.

    // temporary line inside the change handler
    console.log(typeof e.target.value, e.target.value);
  2. 2Match what you see, then jump to its fix

    1. 1You provided a `value` prop to a form field without an `onChange` handlerThe slider is controlled but has no onChangeFix
    2. 2A component is changing an uncontrolled input to be controlledThe state starts as undefined or nullFix
    3. 3The slider moves, but arithmetic with the value gives results like "5010" instead of 60The value arrives as a stringFix
    4. 4The thumb stops at the far right even though the state holds a larger value, or decimals are rounded awaymin, max or step do not fit your numbersFix

What does the error look like?

  • You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
  • A component is changing an uncontrolled input to be controlled

Cause 1 of 4

The slider is controlled but has no onChange

1Check

Passing value makes React the owner of what the input shows. With no handler that updates it, every drag is undone and the field is read-only.

grep -rn 'type="range"' src

2Fix

Keep value and add an onChange that stores the new value. If you only want a starting position, use defaultValue instead.

const [volume, setVolume] = useState(50);

<input
  type="range"
  min={0}
  max={100}
  value={volume}
  onChange={(e) => setVolume(e.target.valueAsNumber)}
/>

// starting position only, no state
<input type="range" defaultValue={50} />

3Verify

Drag the thumb. It follows the pointer, the console warning is gone, and the value read in the console changes.

document.querySelector('input[type=range]').value

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

The state starts as undefined or null

1Check

While value is undefined, React treats the input as uncontrolled. When state later fills in, it flips to controlled and React warns. This happens when the starting number comes from an API or from useState() with no argument.

grep -rn 'useState()' src

2Fix

Give the state a real starting value, or fall back at the point of use. A controlled input must keep getting a value of one type for its whole life.

const [volume, setVolume] = useState(50);

// or, for a value that loads later
<input type="range" value={settings.volume ?? 50} onChange={onChange} />

3Verify

Reload the page. The warning no longer appears when the data arrives, and the input still shows a value.

document.querySelector('input[type=range]').value

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

The value arrives as a string

1Check

The value of a range input is a string holding the number. Adding 10 to the string "50" joins text instead of adding.

console.log(typeof e.target.value);

2Fix

Read the number straight from the element with valueAsNumber, or convert with Number().

onChange={(e) => setVolume(e.target.valueAsNumber)}

// or
onChange={(e) => setVolume(Number(e.target.value))}

3Verify

Log the state after a drag. It should print a number.

console.log(typeof volume, volume + 10);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

min, max or step do not fit your numbers

1Check

Without attributes a range input runs from 0 to 100 in steps of 1. A value above max is set to max, one below min is set to min, and a value that does not fit the step may be rounded.

grep -rn 'type="range"' src | grep -v max

2Fix

Set min, max and step to match the range you really want, including a decimal step for fractions.

<input
  type="range"
  min={0}
  max={500}
  step={0.5}
  value={price}
  onChange={(e) => setPrice(e.target.valueAsNumber)}
/>

3Verify

Set the state to a value inside the new range and read the number back from the element in the console.

document.querySelector('input[type=range]').valueAsNumber

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Treat every input that has a value prop as a pair: value plus onChange, written in the same edit.
  • Initialise slider state with a number of the right type, never undefined or null.
  • Always write min, max and step on a range input, so the numbers in your code and the numbers on screen agree.
  • Convert at the edge: turn the string into a number inside the handler and keep numbers in state.

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

Next step

Want this handled for good?

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