Maaz Zindani

Loading the index…

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

A React checkbox that ignores clicks or never changes state

ErrorYou provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field.

Short answer

A React checkbox that will not toggle is almost always controlled with nothing updating the value. Check the console for the checked-without-onChange warning, make sure checked is always a boolean and the handler reads e.target.checked, and if the state lives in Redux, make sure the reducer returns new objects.

  • Applies to React 19, Redux Toolkit 2, react-redux 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser console and click the checkbox once. A warning here names the cause; no warning points to the Redux and handler causes.

    grep -rn 'type="checkbox"' src/
  2. 2Match what you see, then jump to its fix

    1. 1The console prints that a checked prop was provided without an onChange handler.checked is set but there is no onChangeFix
    2. 2The box flips back, or the state ends up as the string 'on' or the same value every time.The handler reads value instead of checkedFix
    3. 3A component is changing an uncontrolled input to be controlledchecked starts as undefined and later becomes a booleanFix
    4. 4The action shows in Redux DevTools but the checkbox does not re-render.A Redux reducer changes the old state objectFix

What does the error look like?

  • You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field.
  • A component is changing an uncontrolled input to be controlled

Cause 1 of 4

checked is set but there is no onChange

1Check

Look at the checkbox tag. With checked and no handler React renders a read-only field, so the click is ignored.

<input type="checkbox" checked={done} />

2Fix

Add an onChange that updates the state. Use defaultChecked instead if you only need an initial value, or readOnly if the box is meant to be fixed.

<input
  type="checkbox"
  checked={done}
  onChange={e => setDone(e.target.checked)}
/>

3Verify

Click the box. It toggles and the warning is gone from the console.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

The handler reads value instead of checked

1Check

A checkbox reports its state in e.target.checked. Reading e.target.value does not say whether it is ticked.

grep -rn "target.value" src/ | grep -i check

2Fix

Read e.target.checked in the handler and pass checked (not value) to the input.

const [on, setOn] = useState(false);
<input type="checkbox" checked={on} onChange={e => setOn(e.target.checked)} />

3Verify

Log the state after each click. It alternates true and false.

console.log(on)

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

checked starts as undefined and later becomes a boolean

1Check

The value comes from data that has not loaded yet, such as data?.isChecked. React treats undefined as uncontrolled, then complains when it turns into true or false.

<input type="checkbox" checked={data?.isChecked} onChange={handle} />

2Fix

Always pass a boolean.

<input type="checkbox" checked={data?.isChecked ?? false} onChange={handle} />

3Verify

Reload the page. The warning no longer appears while data loads.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

A Redux reducer changes the old state object

1Check

useSelector compares the old and new values with ===. A hand-written reducer that mutates and returns the same object looks unchanged. In a Redux Toolkit store, a mutation error can also be thrown in development.

case 'todos/todoToggled':
  state[0].completed = !state[0].completed
  return state

2Fix

Return new objects at each level you change, or write the reducer with createSlice, where the same code is safe.

case 'todos/todoToggled':
  return state.map(t =>
    t.id === action.payload ? { ...t, completed: !t.completed } : t
  )

3Verify

Click the checkbox. It updates, and the DevTools diff shows a changed value.

npm run dev

Confirmed by redux.js.org

Did this fix it?

How do I stop it coming back?

  • Treat every checked or value prop as a pair with an onChange handler.
  • Give state a real default (false, not undefined) before data loads.
  • Use createSlice for reducers so accidental mutation cannot happen.

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.