Skip to main content
Clones a virtual node (VNode) and optionally merges new props or replaces its children.

Signature

Parameters

VNode
required
The virtual node to clone. Must be a valid VNode created with h() or createElement().
object
Additional props to merge with the original VNode’s props. Special props like key and ref are handled separately.
ComponentChildren
New children to replace the original VNode’s children. If provided, these replace (not merge with) the original children.

Return Value

Returns a new VNode with:
  • Same type as the original
  • Merged props (original + new)
  • New children if provided, otherwise original children
  • New key if provided, otherwise original key
  • New ref if provided, otherwise original ref

Description

cloneElement creates a shallow copy of a virtual node, allowing you to modify its props and children. This is useful for:
  • Wrapping or modifying elements passed as children
  • Adding props to elements dynamically
  • Implementing higher-order components
  • Extending component functionality
The function preserves the original VNode’s type while merging new props and optionally replacing children.

Implementation

The function is implemented in src/clone-element.js:14:

Usage Examples

Basic Cloning

Adding Props to Children

Replacing Children

Merging Props

Extending Component Props

Working with React.Children Pattern

Adding Event Handlers

Updating Keys

Modifying Refs

Higher-Order Component Pattern

Conditionally Cloning

With TypeScript

Important Behaviors

Props Merging

New props are merged with original props, not replaced:

Children Replacement

Children are replaced, not merged:

Key and Ref Handling

key and ref are special props:

Shallow Clone

cloneElement performs a shallow clone:

Common Pitfalls

Not Preserving Original Handlers

Cloning Non-VNodes

Performance Considerations

  • cloneElement creates a new VNode object
  • Original VNode is not modified (immutable)
  • Minimal overhead - only copies necessary properties
  • Use sparingly in performance-critical paths
  • h - Create virtual nodes
  • createElement - React-compatible node creation
  • toChildArray - Convert children to array for mapping
  • isValidElement - Check if value is a VNode