Skip to main content
These APIs allow you to create and work with React elements programmatically. Most of the time you’ll use JSX instead, but these functions are useful when working with element trees dynamically.

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 lists
  • ref - Ref to access the underlying DOM node or component instance
  • Special props like className, style, event handlers, etc.
Pass 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, or false (rendered as nothing)
  • Arrays of any of the above
Returns: A React element object.

Usage

Basic element creation:
Multiple children:
Component creation:
With key and ref:
Dynamic element creation:

Default Props

If the component has defaultProps 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 via props.key)
  • ref - Used to access DOM nodes or component instances (treated specially in React 19+)
  • __self and __source - Used internally by React DevTools (should not be used in application code)

JSX Transformation

JSX is transformed to createElement calls by the compiler:
React 17+ uses a new JSX transform that doesn’t require React.createElement. The above transformation only applies to the classic JSX runtime.

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.
  • key and ref can be overridden
  • If ref is provided, the owner will be updated
  • undefined ref 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.
Returns: A new React element with merged props and replaced children (if provided). Throws: Error if the element argument is null or undefined.

Usage

Basic cloning:
Overriding props:
Replacing children:
Adding props to children:
Preserving keys:
Handling refs:

When to Use cloneElement

Use cloneElement 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
Prefer alternatives when:
  • 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 createElement or JSX)
cloneElement is somewhat rare in modern React code. Consider using render props, context, or composition patterns instead when possible, as they’re often more maintainable.

Example: Enhancing Children

isValidElement()

Verify whether an object is a React element.
any
required
Any JavaScript value to test.
Returns: true if the object is a valid React element, false otherwise.

Usage

Basic validation:
Type checking in functions:
Filtering elements:
Conditional rendering helper:

What Makes a Valid Element

A valid React element is an object with:
These objects are created by:
  • JSX syntax: <div />
  • createElement(): createElement('div')
  • cloneElement(): cloneElement(element)

Common Use Cases

Validating props.children:
Building flexible APIs:
Safe element manipulation:
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.