Skip to content

Checkbox: replace useLayoutEffect with ref callback for indeterminate prop #7764

Description

@alexus37

Summary

The Checkbox component currently uses a useLayoutEffect to imperatively set the DOM .indeterminate property on the underlying <input> element. While this works, it can be replaced with a ref callback, which runs synchronously during commit (same timing guarantees as useLayoutEffect) without registering a separate effect.

Current behavior

useLayoutEffect(() => {
  if (checkboxRef.current) {
    checkboxRef.current.indeterminate = indeterminate || false
  }
}, [indeterminate, checked, checkboxRef])

Proposed change

Replace with a ref callback that sets .indeterminate inline:

const setIndeterminate = React.useCallback(
  (node: HTMLInputElement | null) => {
    if (node) {
      node.indeterminate = indeterminate || false
    }
  },
  [indeterminate, checked],
)

// merge with the forwarded ref
const mergedRef = useMergedRef(checkboxRef, setIndeterminate)

Additional cleanup

The useEffect that sets aria-checked can also be replaced with an inline JSX attribute:

'aria-checked': indeterminate ? 'mixed' : undefined

This avoids an unnecessary effect per Checkbox instance.

Motivation

  • Ref callbacks are the idiomatic React pattern for imperative DOM properties with no HTML attribute equivalent
  • Eliminates a layout effect per Checkbox render, reducing React commit phase work
  • Minor but measurable improvement in pages that render many checkboxes (e.g., list views with bulk selection)

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions