Skip to main content
Creates a virtual node (VNode) representing a DOM element or component. This is Preact’s React-compatible API for creating virtual DOM nodes.

Signature

Parameters

string | ComponentType<P>
required
The element type. Can be:
  • A string for HTML/SVG elements (e.g., 'div', 'span', 'svg')
  • A function component
  • A class component
object | null
An object containing the element’s properties/attributes. Special props:
  • key: Unique identifier for efficient reconciliation
  • ref: Reference to the DOM node or component instance
  • All other props are passed to the component or set as attributes
ComponentChildren
Child elements. Can be:
  • VNodes created with createElement()
  • Strings or numbers (rendered as text)
  • Arrays of children
  • null, undefined, or booleans (not rendered)

Return Value

Returns a VNode<P> object representing the virtual DOM node.

Description

createElement is Preact’s React-compatible function for creating virtual DOM nodes. It’s identical to the h function but uses the React naming convention. This function is the target of JSX transformations when using the React JSX pragma. It can also be called directly for creating virtual nodes programmatically.

Implementation

The function is implemented in src/create-element.js:16:

Usage Examples

JSX with React Pragma

Direct Function Calls

Creating Components

With Props and Event Handlers

Lists with Keys

Using Refs

Conditional Rendering

Spreading Props

Special Behaviors

Key Prop

The key prop is extracted and not passed to the component:

Ref Prop

The ref prop is extracted for DOM elements but passed through for function components:

Children Normalization

Children are automatically collected from all arguments after props:

JSX Configuration

Configure JSX to use createElement with React-compatible settings: TypeScript (tsconfig.json)
Babel (.babelrc)
Or use automatic runtime (React 17+)

Performance Notes

  • The function normalizes props efficiently by only copying non-special props
  • Variadic children arguments are only allocated when multiple children exist
  • VNode creation is optimized with object shape consistency
  • h - Identical function with hyperscript naming
  • Fragment - Render multiple children without a wrapper
  • cloneElement - Clone and modify existing VNodes
  • render - Render VNodes to the DOM