Signature
Parameters
The element type. Can be:
- A string for HTML/SVG elements (e.g.,
'div','span','svg') - A function component
- A class component
An object containing the element’s properties/attributes. Special props:
key: Unique identifier for efficient reconciliationref: Reference to the DOM node or component instance- All other props are passed to the component or set as attributes
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 aVNode<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 insrc/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
Thekey prop is extracted and not passed to the component:
Ref Prop
Theref 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 usecreateElement with React-compatible settings:
TypeScript (tsconfig.json)
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
Related APIs
h- Identical function with hyperscript namingFragment- Render multiple children without a wrappercloneElement- Clone and modify existing VNodesrender- Render VNodes to the DOM