Skip to content

Table components: Refs are not set in first renderΒ #9325

@jdharrisnz

Description

@jdharrisnz

Provide a general summary of the issue here

When using refs with Table components, the data isn't set on first render. Minimal example:

import { useEffect, useRef } from "react"
import {
  Cell,
  Column,
  Row,
  Table,
  TableBody,
  TableHeader,
} from "react-aria-components"

import type { ColumnProps } from "react-aria-components"

const MyColumn = (props: ColumnProps) => {
  const ref = useRef<HTMLTableCellElement>(null)

  useEffect(() => {
    console.log(ref) // Logs "{ current: null }"
  }, [])

  return <Column ref={ref} {...props} />
}

const Component = () => (
  <Table>
    <TableHeader>
      <MyColumn>Hello world</MyColumn>
    </TableHeader>
    <TableBody>
      <Row>
        <Cell>A cell</Cell>
      </Row>
    </TableBody>
  </Table>
)

There is a slightly hacky workaround that works. React state setters satisfy the RefCallback type, so you can just make it reactive and do this:

const MyColumn = (props: ColumnProps) => {
  const [ref, setRef] = useState<HTMLTableCellElement | null>(null)

  useEffect(() => {
    console.log(ref)
  }, [ref])

  return <Column ref={setRef} {...props} />
}

But if this is the solution, it should really be mentioned in the docs or supported explicitly with a custom useTableRef hook.

πŸ€” Expected Behavior?

Refs should be available in effects on first render.

😯 Current Behavior

Ref is null on first render.

πŸ’ Possible Solution

  1. If this is caused by Table's computation model introducing weird timing, look into adjusting that
  2. If 1 is not possible, document the behaviour and/or export a custom useTableRef hook to help with it

πŸ”¦ Context

Standard ref usage with Table components doesn't work, and we need strange workarounds to fit with it.

πŸ–₯️ Steps to Reproduce

https://codesandbox.io/p/devbox/zealous-joana-l7dvvy

Version

"react-aria-components": "^1.13.0"

What browsers are you seeing the problem on?

Chrome, Safari

If other, please specify.

No response

What operating system are you using?

macOS 26.1

🧒 Your Company/Team

No response

πŸ•· Tracking Issue

No response

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