-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Description
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
- If this is caused by Table's computation model introducing weird timing, look into adjusting that
- If 1 is not possible, document the behaviour and/or export a custom
useTableRefhook 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
Labels
No labels