Skip to content

mergeProps

Merges React prop objects, chaining event handlers and joining classes and styles.

mergeProps merges several sets of React props into one. It is Base UI's mergeProps re-exported through a Dify UI subpath, for composing a part's own props with the props a caller passes.

tsx

Usage guidelines

  • Use it when a part adds its own props to the caller's: event handlers from both sides keep running, and className and style combine instead of one replacing the other.
  • Pass the caller's props last: for plain props, the rightmost object wins, so the caller can override a default such as rel or type.
  • Resolve Tailwind conflicts with cn: mergeProps concatenates class strings without resolving conflicting utilities. Merge default and caller classes with cn first, as in the example below.
  • Merge refs separately: ref is not merged. Combine refs with useMergedRefs.
  • Do not use it for a render element: Base UI already merges a part's props into the element passed to render. useRender also merges its props with the render element.

Each argument accepts React props, a function receiving the previously merged props, or undefined.

The merge rules:

  • Plain props follow Object.assign: the rightmost value overwrites the others.
  • Event handlers are chained and run right to left, so the rightmost handler runs first. For a React synthetic event, it can call event.preventBaseUIHandler() to stop the handlers to its left.
  • className strings are concatenated right to left. style objects are merged with the rightmost values winning.
  • A function argument receives the props merged so far and returns new props. It does not run the merge logic, so it chains handlers itself.
  • ref is not merged.

Examples

The part's defaults come first and the caller's props last, so a caller-supplied rel or onClick still applies.

tsx

API reference

mergeProps

Merges multiple sets of React props. It follows the Object.assign pattern where the rightmost object's fields overwrite the conflicting ones from others. This doesn't apply to event handlers, className and style props.

Event handlers are merged and called in right-to-left order (rightmost handler executes first, leftmost last). For React synthetic events, the rightmost handler can prevent prior (left-positioned) handlers from executing by calling event.preventBaseUIHandler(). For non-synthetic events (custom events with primitive/object values), all handlers always execute without prevention capability.

The className prop is merged by concatenating classes in right-to-left order (rightmost class appears first in the string). The style prop is merged with rightmost styles overwriting the prior ones.

Props can either be provided as objects or as functions that take the previous props as an argument. The function will receive the merged props up to that point (going from left to right): so in the case of (obj1, obj2, fn, obj3), fn will receive the merged props of obj1 and obj2. The function is responsible for chaining event handlers if needed (that is, we don't run the merge logic).

Event handlers returned by the functions are not automatically prevented when preventBaseUIHandler is called. They must check event.baseUIHandlerPrevented themselves and bail out if it's true.

Shared parameters
ParameterTypeDescription
a requiredInputProps<T>

Props object to merge.

b requiredInputProps<T>

Props object to merge. The function will overwrite conflicting props from a.

Returns

WithBaseUIEvent<React.ComponentPropsWithRef<T>>

The merged props.