Skip to content
Source
Sign inSign up
TanStack/redact

Controlled checkbox reverts when another state update happens during the click #127

OpenMichael-1(GitHub profile, opens in a new tab) opened this issue 2 comments

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.7Redact 0.2.3
After the 1st clickChecked: trueChecked: false
After the 2nd clickChecked: falseChecked: 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.

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

    AI review notes: The reported case passes through the real Vitest Playwright provider on the candidate and native React, and fails on the unchanged runtime. The current candidate also passes all 2,188 tests, types, build verification and the full 73-check configured browser suite on Linux Chromium. Equivalent-work timings across three engines showed much faster deep bubbling, with about 0.5 to 2 extra microseconds for a single-handler click. Bundle and bounded heap measurements are included in the owner decision. These are fixture measurements, not app-wide performance claims. The candidate is still local, its size checks remain closed pending the exact decision. This issue stays open, nothing is merged or released for it yet.

  2. Another case with the same cause, where both listeners are React props on one element. A controlled text input has an onInput that sets unrelated state and an onChange that sets the value. Redact attaches each prop as its own native listener, so the render from onInput's update commits in the microtask between the two listeners. It writes the old value back before onChange reads event.target.value. Text that arrives in one input event with no keydown before it, such as Playwright's fill() 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.8Redact 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 in onInput, and worked around it by doing both in onChange. 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.