useRender
TheuseRender hook is used internally by Base UI components to render elements with support for custom render props, state attributes, and ref handling.
Import
Usage
Basic usage with default rendering:Custom render prop
Override the default element with a custom render function:State attributes
Automatically convert state to data attributes:API Reference
Parameters
TheuseRender hook accepts a single parameters object with the following properties:
defaultTagName
- Type:
keyof React.JSX.IntrinsicElements - Default:
'div' - Optional
render is not provided.
render
- Type:
React.ReactElement | ((props: RenderFunctionProps, state: State) => React.ReactElement) - Optional
props: Props to be spread on the rendered elementstate: The component state object
ref
- Type:
React.Ref<Element> | React.Ref<Element>[] - Optional
state
- Type:
Record<string, unknown> - Optional
render callback. State properties are automatically converted to data-* attributes.
stateAttributesMapping
- Type:
Record<string, (value: unknown) => Record<string, string> | null> - Optional
props
- Type:
Record<string, unknown> - Optional
- Event handlers (merged)
className(concatenated)style(merged)- Other props (external props overwrite internal)
enabled
- Type:
boolean - Default:
true - Optional
false, the hook will skip most of its internal logic and return null. This is useful for conditional rendering.
Return Value
Returns aReact.ReactElement, or null if enabled is false.
Use Cases
Building headless components
UseuseRender to build headless components that support custom rendering:
Conditional rendering
Use theenabled parameter for conditional rendering: