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 newVNode with:
- Same
typeas the original - Merged props (original + new)
- New children if provided, otherwise original children
- New
keyif provided, otherwise original key - New
refif 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
Implementation
The function is implemented insrc/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
cloneElementcreates a new VNode object- Original VNode is not modified (immutable)
- Minimal overhead - only copies necessary properties
- Use sparingly in performance-critical paths
Related APIs
h- Create virtual nodescreateElement- React-compatible node creationtoChildArray- Convert children to array for mappingisValidElement- Check if value is a VNode