Skip to contents

mergeProps

A utility to merge multiple sets of React props.

View as Markdown

mergeProps 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' }
  • ref is not merged. Only the rightmost ref is kept:

    only refB is used
  • className values are concatenated right-to-left (rightmost first):

    className is 'b a'
  • style objects 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 a
    • For React synthetic events, Base UI adds event.preventBaseUIHandler(). Calling it prevents Base UI’s internal logic from running. This does not call preventDefault() or stopPropagation().
    • For non-synthetic events (custom events with primitive/object values), this mechanism isn’t available and all handlers always execute.

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:

Favorite (locked)

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:

Manually chaining handlers in a function

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

{}
The merged props.

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>[]

Return value

{}
The merged props.