createElement()
Create and return a new React element of the given type.string | ComponentType
required
The type of element to create:
- A string for HTML elements (
'div','span', etc.) - A React component (function or class)
- A React fragment (
React.Fragment) - Or other special types (Suspense, StrictMode, etc.)
object | null
An object containing props to pass to the element. Can include:
- Any custom props
key- String or number to identify the element in listsref- Ref to access the underlying DOM node or component instance- Special props like
className,style, event handlers, etc.
null or omit if the element has no props.ReactNode
Zero or more child elements. Can be:
- Other React elements
- Strings or numbers (rendered as text)
null,undefined, orfalse(rendered as nothing)- Arrays of any of the above
Usage
Basic element creation:Default Props
If the component hasdefaultProps defined, createElement will merge them with the provided props:
Special Props
These props have special meaning to React:
key- Used to identify elements in lists (not accessible viaprops.key)ref- Used to access DOM nodes or component instances (treated specially in React 19+)__selfand__source- Used internally by React DevTools (should not be used in application code)
JSX Transformation
JSX is transformed tocreateElement calls by the compiler:
cloneElement()
Clone and return a new React element using an existing element as the starting point.ReactElement
required
The React element to clone. Must be a valid React element, not
null or undefined.object | null
Props to merge with the original element’s props. Props from this object will override props from the original element.
keyandrefcan be overridden- If
refis provided, the owner will be updated undefinedref values are ignored (original ref is preserved)
ReactNode
Zero or more children to replace the original element’s children. If provided, these completely replace the original children.
Error if the element argument is null or undefined.
Usage
Basic cloning:When to Use cloneElement
UsecloneElement when:
- You need to inject props into children passed as
props.children - You’re building higher-order components or component wrappers
- You need to modify or enhance existing elements
- You can pass props directly to components (more explicit)
- You can use render props or context (more maintainable)
- You’re just creating new elements (use
createElementor JSX)
Example: Enhancing Children
isValidElement()
Verify whether an object is a React element.any
required
Any JavaScript value to test.
true if the object is a valid React element, false otherwise.
Usage
Basic validation:What Makes a Valid Element
A valid React element is an object with:- JSX syntax:
<div /> createElement():createElement('div')cloneElement():cloneElement(element)
Common Use Cases
Validating props.children:isValidElement only checks if something is a React element. It doesn’t validate if the element type is correct, if props are valid, or if the element will render successfully.