Skip to main content
Creates a virtual node (VNode) representing a DOM element or component. This is Preact’s hyperscript function, commonly used as the JSX pragma.

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 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 h()
  • 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

h is Preact’s hyperscript function, used to create virtual DOM nodes. It’s the target function for JSX transformations and can also be called directly. The function is an alias for createElement and has identical behavior. The name h is shorter and commonly used in hyperscript-style code.

Usage Examples

JSX Pragma

Direct Function Calls

With Props and Children

Creating Component VNodes

With Key and Ref

Multiple Children

Arrays of Children

Implementation Details

The h function is implemented in src/create-element.js:16 and shares the same implementation as createElement. The function:
  1. Normalizes props by extracting key and ref
  2. Collects all children arguments (supports variadic arguments)
  3. Calls internal createVNode to create the VNode object

Special Props

key

Used by the diffing algorithm to identify elements across renders:

ref

Provides access to the underlying DOM node:

dangerouslySetInnerHTML

Sets raw HTML content (use with caution):

JSX Configuration

Configure your JSX transform to use h: TypeScript (tsconfig.json)
Babel (.babelrc)