Controlled checkbox reverts when another state update happens during the click #127
A controlled checkbox can’t be checked when something else updates state from a native click listener, e.g. a click-away listener like the one in MUI’s Snackbar. Redact flushes that update in a microtask before the browser fires change, re-renders the checkbox with the old checked and writes it back to the DOM. The change event then reports the old value, so onChange keeps the box unchecked.
Reproduction#
import {useEffect, useState} from 'react'
import {createRoot} from 'react-dom/client'
const App = () => {
const [clicks, setClicks] = useState(0)
const [checked, setChecked] = useState(false)
useEffect(() => {
const onClick = () => setClicks((n) => n + 1)
document.addEventListener('click', onClick)
return () => document.removeEventListener('click', onClick)
}, [])
return (
<label>
<input
type="checkbox"
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>
Checked: {String(checked)}, document clicks: {clicks}
</label>
)
}
createRoot(document.getElementById('root')).render(<App />)
Click the checkbox:
| React DOM 19.2.7 | Redact 0.2.3 | |
|---|---|---|
| After the 1st click | Checked: true | Checked: false |
| After the 2nd click | Checked: false | Checked: false |
On Redact the box never stays checked. Tested in Chromium with Vite and the default redact() plugin.
Expected#
As in React DOM, the checkbox keeps the value the browser toggled until change has fired. #116 touches the same code (restoring controlled state after change), but this case calls setState and fails before onChange runs.
Another case with the same cause, where both listeners are React props on one element. A controlled text input has an
onInputthat sets unrelated state and anonChangethat sets the value. Redact attaches each prop as its own native listener, so the render fromonInput's update commits in the microtask between the two listeners. It writes the oldvalueback beforeonChangereadsevent.target.value. Text that arrives in oneinputevent with no keydown before it, such as Playwright'sfill()or an IME commit, is lost. Typing works, because nothing updates state between the two listeners.import {useState} from 'react' export function TwoHandlers() { const [value, setValue] = useState('') const [touched, setTouched] = useState(false) return ( <> <input aria-label="q" value={value} onInput={() => setTouched(true)} onChange={(e) => setValue(e.target.value)} /> <output>{touched ? `touched:${value}` : 'untouched'}</output> </> ) }After
await page.getByLabel('q').fill('test'):React DOM 19.2.8 Redact 0.3.1 <output>touched:testtouched:Vitest browser mode with Playwright 1.62.0 on Chromium, default
redact()plugin. We hit it in a Headless UI Combobox search box that marks the first interaction inonInput, and worked around it by doing both inonChange. If the combined event fix dispatches all of an element's handlers for one native event before flushing, it should cover this case too.
Sign in to join this conversation.
Thanks for the report, the browser regression now passes with the combined event fix. The remaining size and runtime tradeoffs have been sent for a decision.