mergeProps
A utility to merge multiple sets of React props.
View as MarkdownmergeProps helps you combine multiple prop objects (for example, internal props + user props) into a single set of props you can spread onto an element.
It behaves like Object.assign (rightmost wins) with a few special cases, so common React patterns work as expected.
How merging works
-
For most keys (everything except
className,style, and event handlers), the value from the rightmost object wins:returns { id: 'b', dir: 'ltr' }mergeProps({ id: 'a', dir: 'ltr' }, { id: 'b' }); -
refis not merged. Only the rightmost ref is kept:only refB is usedmergeProps({ ref: refA }, { ref: refB }); -
classNamevalues are concatenated right-to-left (rightmost first):className is 'b a'mergeProps({ className: 'a' }, { className: 'b' }); -
styleobjects are merged, with keys from the rightmost style overwriting earlier ones. -
Event handlers are merged and executed right-to-left (rightmost first):
b runs before amergeProps({ onClick: a }, { onClick: b });- For React synthetic events, Base UI adds
event.preventBaseUIHandler(). Calling it prevents Base UI’s internal logic from running. This does not callpreventDefault()orstopPropagation(). - For non-synthetic events (custom events with primitive/object values), this mechanism isn’t available and all handlers always execute.
- For React synthetic events, Base UI adds
Preventing Base UI’s default behavior
When using the function form of the render prop, props are not merged automatically.
You can use mergeProps to combine Base UI’s props with your own, and call preventBaseUIHandler() to stop Base UI’s internal logic from running:
Passing a function instead of an object
Each argument can be a props object or a function that receives the merged props up to that point (left to right) and returns a props object. This is useful when you need to compute the next props from whatever has already been merged.
Note that the function’s return value completely replaces the accumulated props up to that point. If you want to chain event handlers from the previous props, you must call them manually:
const merged = mergeProps(
{
onClick(event) {
// Handler from previous props
},
},
(props) => ({
onClick(event) {
// Manually call the previous handler
props.onClick?.(event);
// Your logic here
},
}),
);API reference
mergeProps
This function accepts up to 5 arguments, each being either a props object or a function that returns a props object.
If you need to merge more than 5 sets of props, use mergePropsN instead.
Parameters
a*InputProps<ElementType>—
- Name
- Description
Props object to merge.
- Type
InputProps<ElementType>
b*InputProps<ElementType>—
- Name
- Description
Props object to merge. The function will overwrite conflicting props from
a.- Type
InputProps<ElementType>
cInputProps<ElementType>—
- Name
- Description
Props object to merge. The function will overwrite conflicting props from previous parameters.
- Type
InputProps<ElementType>
dInputProps<ElementType>—
- Name
- Description
Props object to merge. The function will overwrite conflicting props from previous parameters.
- Type
InputProps<ElementType>
eInputProps<ElementType>—
- Name
- Description
Props object to merge. The function will overwrite conflicting props from previous parameters.
- Type
InputProps<ElementType>
Return value
{}
mergePropsN
This function accepts an array of props objects or functions that return props objects.
It is slightly less efficient than mergeProps, so only use it when you need to merge more than 5 sets of props.
Parameters
props*InputProps<ElementType>[]—
- Name
- Description
Array of props to merge.
- Type
InputProps<ElementType>[]