useRender
Hook for enabling a render prop in custom components.
The useRender hook lets you build custom components that provide a render prop to override the default rendered element.
Examples
A render prop for a custom Text component lets consumers use it to replace the default rendered p element with a different tag or component.
Text component rendered as a paragraph tag
Text component rendered as a strong tagimport { omit } from 'solid-js';
import { useRender } from 'base-ui-solid/use-render';
import { mergeProps } from 'base-ui-solid/merge-props';
import styles from './index.module.css';
interface TextProps extends useRender.ComponentProps<'p'> {}
function Text(props: TextProps) {
const otherProps = omit(props, 'render');
const element = useRender({
defaultTagName: 'p',
get render() {
return props.render;
},
props: () => mergeProps<useRender.ElementProps<'p'>>({ class: styles.Text }, otherProps),
});
return element;
}
export default function ExampleText() {
return (
<div>
<Text>Text component rendered as a paragraph tag</Text>
<Text render="strong">Text component rendered as a strong tag</Text>
</div>
);
}
The callback version of the render prop enables more control of how props are spread, and also passes the internal state of a component.
import { createSignal, createMemo, omit } from 'solid-js';
import { useRender } from 'base-ui-solid/use-render';
import { mergeProps } from 'base-ui-solid/merge-props';
import styles from './index.module.css';
interface CounterState {
odd: boolean;
}
interface CounterProps extends useRender.ComponentProps<'button', CounterState> {}
function Counter(props: CounterProps) {
const otherProps = omit(props, 'render');
const [count, setCount] = createSignal(0);
const odd = () => count() % 2 === 1;
const state = createMemo(() => ({ odd: odd() }));
const defaultProps: useRender.ElementProps<'button'> = {
class: styles.Button,
type: 'button',
children: (
<div style={{ display: 'contents' }}>
Counter: <span class={styles.count}>{count()}</span>
</div>
),
onClick() {
setCount((prev) => prev + 1);
},
get 'aria-label'() {
return `Count is ${count()}, click to increase.`;
},
};
const element = useRender({
defaultTagName: 'button',
get render() {
return props.render;
},
state,
props: () => mergeProps(defaultProps, otherProps),
});
return element;
}
export default function ExampleCounter() {
return (
<Counter
render={(props, state) => (
<button {...props}>
{props.children}
<span class={styles.suffix}>{state.odd ? '👎' : '👍'}</span>
</button>
)}
/>
);
}
Merging props
The mergeProps function merges two or more sets of Solid props together. It safely merges four types of props:
- Event handlers, so that all are invoked
classvaluesstyleproperties- Ref callbacks
mergeProps merges objects from left to right, so that subsequent objects’ properties in the arguments overwrite previous ones. Merging props is useful when creating custom components, as well as inside the callback version of the render prop for any Base UI component.
import { mergeProps } from 'base-ui-solid/merge-props';
import styles from './index.module.css';
function Button() {
return (
<Component
render={(props, state) => (
<button
{...mergeProps(props, {
class: styles.Button,
})}
/>
)}
/>
);
}
Merging refs
When building custom components, you often need to control a ref internally while still letting external consumers pass their own—merging refs lets both parties have access to the underlying DOM element. The ref option in useRender enables this, which holds an array of refs to be merged together.
In Solid 2.0, refs are callbacks passed through props. No forwardRef wrapper is needed. Pass an internal ref callback together with the consumer’s ref:
import { omit } from 'solid-js';
function Text(props: TextProps) {
let internalElement: HTMLElement | undefined;
return useRender({
defaultTagName: 'p',
get render() {
return props.render;
},
ref: [
(element) => {
internalElement = element;
},
props.ref,
],
props: omit(props, 'render', 'ref'),
});
}
TypeScript
To type props, there are two interfaces:
useRender.ComponentPropsfor a component’s external (public) props. It types therenderprop and HTML attributes.useRender.ElementPropsfor the element’s internal (private) props. It types HTML attributes alone.
interface ButtonProps extends useRender.ComponentProps<'button'> {}
function Button(componentProps: ButtonProps) {
const props = omit(componentProps, 'render');
const defaultProps: useRender.ElementProps<'button'> = {
class: styles.Button,
type: 'button',
children: 'Click me',
};
const element = useRender({
defaultTagName: 'button',
get render() {
return componentProps.render;
},
props: () => mergeProps(defaultProps, props),
});
return element;
}
Migrating from Radix UI
Radix UI uses an asChild prop, while Base UI uses a render prop. Learn more about how composition works in Base UI in the composition guide.
In Radix UI, the Slot component lets you implement an asChild prop.
import { Slot } from 'radix-ui';
function Button({ asChild, ...props }) {
const Comp = asChild ? Slot.Root : 'button';
return <Comp {...props} />;
}
// Usage
<Button asChild>
<MyButton class="primary">Submit</MyButton>
</Button>;
In Base UI, useRender lets you implement a render prop. The example below is the equivalent implementation to the Radix example above.
import { useRender } from 'base-ui-solid/use-render';
function Button(componentProps) {
const props = omit(componentProps, 'render');
return useRender({
defaultTagName: 'button',
get render() {
return componentProps.render;
},
props,
});
}
// Usage
<Button render={(props) => <MyButton class="primary" {...props} />}>Submit</Button>;
Render prop and polymorphism
The render prop is primarily designed for composing event handlers and behavioral props. In most cases it should render the same tag as the default element.
Using render for polymorphism (rendering a different tag) requires more care, as some default props may not be valid on the new element. For example, type="button" is only valid on a <button>. Since the component can’t know what element render will produce at render time and before hydration, props like these need an explicit signal. This is why Base UI’s Button provides a nativeButton prop to control which defaults are applied.
API reference
const element = useRender({
// Input parameters
});
paramsuseRender.Parameters<Record<string, unknown>>—
useRender.Parameters<Record<string, unknown>>Solid useRender types
export type UseRenderRenderProp<State = Record<string, unknown>> =
ComponentRenderFn<HTMLProps, State> | IntrinsicTagName;
export type UseRenderElementProps<ElementType extends IntrinsicTagName> =
JSX.IntrinsicElements[ElementType];
export type UseRenderComponentProps<
ElementType extends IntrinsicTagName,
State = {},
RenderFunctionProps = HTMLProps,
> = JSX.IntrinsicElements[ElementType] & {
/**
* Allows you to replace the component's HTML element
* with a different tag, or compose it with another component.
*
* Accepts a tag name, a component, or a function that returns the element to render.
*/
render?: ComponentRenderFn<RenderFunctionProps, State> | IntrinsicTagName | undefined;
};
export interface UseRenderParameters<State> {
/**
* The tag name, component, or function returning the element to override the default element.
*/
render?: UseRenderRenderProp<State> | undefined;
/**
* The ref (or refs) to apply to the rendered element.
*/
ref?: ((element: any) => void) | Array<((element: any) => void) | undefined> | undefined;
/**
* The state of the component, passed as the second argument to the `render` callback.
* State properties are automatically converted to data-* attributes.
* Pass an accessor or a reactive object to keep it up to date.
*/
state?: State | Accessor<State> | undefined;
/**
* Custom mapping for converting state properties to data-* attributes.
* @example
* { isActive: (value) => (value ? { 'data-is-active': '' } : null) }
*/
stateAttributesMapping?: StateAttributesMapping<State> | undefined;
/**
* Props to be spread on the rendered element.
* They are merged with the internal props of the component, so that event handlers
* are merged, `class` values and `style` properties are joined, while other external props overwrite the
* internal ones.
*/
props?: Record<string, unknown> | Accessor<Record<string, unknown>> | undefined;
/**
* If `false`, the element is not rendered.
* This is useful for rendering a component conditionally.
* @default true
*/
enabled?: boolean | Accessor<boolean> | undefined;
/**
* The default tag name to use for the rendered element when `render` is not provided.
* @default 'div'
*/
defaultTagName?: IntrinsicTagName | undefined;
/**
* The class to apply to the rendered element.
*/
class?: ClassProp<State> | undefined;
/**
* The style to apply to the rendered element.
*/
style?: StyleProp<State> | undefined;
}
export type UseRenderReturnValue = JSX.Element;
export interface UseRenderState {}
export namespace useRender {
export type State = UseRenderState;
export type RenderProp<TState = Record<string, unknown>> = UseRenderRenderProp<TState>;
export type ElementProps<ElementType extends IntrinsicTagName> =
UseRenderElementProps<ElementType>;
export type ComponentProps<
ElementType extends IntrinsicTagName,
TState = {},
RenderFunctionProps = HTMLProps,
> = UseRenderComponentProps<ElementType, TState, RenderFunctionProps>;
export type Parameters<TState> = UseRenderParameters<TState>;
export type ReturnValue = UseRenderReturnValue;
}