useExpanded
useExpanded
is the hook that implements row expanding. It is most often used with useGroupBy
to expand grouped rows or on its own with nested subRows
in tree-like data
sets, but is not limited to these use-cases. It supports expanding rows both via internal table state and also via a hard-coded key on the raw row model.
The following options are supported via the main options object passed to useTable(options)
initialState.expanded: Object<rowId: String, expanded: Bool>
object
of expanded row IDs with boolean property values.{ '3': true }
was passed as the expanded
state, by default the 4th row in the original data array would be expanded, since it would have that ID1.3
to expand sub rows. For example, if { '3': true, '3.5': true }
was passed as the expanded
state, then the the 4th row would be expanded, along with the 6th subRow of the 4th row as well.getSubRows: Function(row, relativeIndex) => Rows[]
manualExpandedKey: String
expanded
{ name: 'Tanner Linsley', friends: [...], expanded: true}
was detected, it would always be expanded, regardless of state.expandSubRows: Bool
true
true
, expanded rows are rendered along with normal rows.false
, expanded rows will only be available through their parent row. This could be useful if you are implementing a custom expanded row view.autoResetExpanded: Boolean
true
true
, the expanded
state will automatically reset if any of the following conditions are met:data
is changedfalse
The following properties are available on the table instance returned from useTable
rows: Array<Row>
toggleRowExpanded: Function(rowId, isExpanded?)
isExpanded
boolean is optional, otherwise it will be a true toggle actiontoggleAllRowsExpanded: Function(isExpanded?)
isExpanded
boolean is optional, otherwise it will be a true toggle actionisAllRowsExpanded
getToggleAllRowsExpandedProps: Function(userProps) => props
The following additional properties are available on every row
object returned by the table instance.
isExpanded: Bool
true
, this row is in an expanded state.toggleRowExpanded: Function(?isExpanded: Bool) => void
true
and false
or, if an isExpanded
boolean is passed to the function, it will be set as the new isExpanded
value.manualExpandedKey
(defaults to expanded
) set to true
are not affected by this function or the internal expanded state.The best JavaScript newsletter! Delivered every Monday to over 76,000 devs.