Skip to content

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

Open

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

  1. added a commit that references this issue on Apr 17, 2026
    0464be5
  2. akshat-lakhera commented on Oct 20, 2026

    @akshat-lakhera

    Hi, is this still open for someone to pick up? In packages/react/src/Checkbox/Checkbox.tsx the indeterminate state is set inside a useLayoutEffect, and I'd move it into a ref callback merged through useMergedRefs and drop the separate aria-checked effect. I noticed #8548 is graduating the merged forwarded refs flag in the same file, so I'd build on top of that once it lands. Could you assign it to me?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions