Skip to main content

mergeProps

The mergeProps function intelligently merges multiple sets of React props, with special handling for event handlers, className, and style properties.

Import

Usage

Merge two or more prop objects:

Merging behavior

Event handlers

Event handlers are merged and called in right-to-left order (rightmost handler executes first):

Preventing handler execution

For React synthetic events, you can prevent earlier handlers from executing:

className merging

Class names are concatenated in right-to-left order (rightmost class appears first):

style merging

Style objects are merged with rightmost styles taking precedence:

API Reference

Parameters

mergeProps accepts 2 to 5 prop objects or functions:
Each parameter can be:
  • A props object
  • A function that receives the merged props so far and returns a props object
  • undefined

Function props

You can pass functions that receive the merged props:

Return value

Returns a merged props object with all properties combined according to the merging rules. Important: The ref prop is not merged and will be overwritten by the rightmost value.

mergePropsN

For merging more than 5 prop objects, use mergePropsN which accepts an array:
Note: mergePropsN has slightly lower performance than mergeProps due to accepting an array. Prefer mergeProps when merging 5 or fewer prop sets.

Use Cases

Building custom components

Merge internal component props with user-provided props:

Composing hooks

Combine props from multiple hooks: