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 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
h() - 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
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
Theh function is implemented in src/create-element.js:16 and shares the same implementation as createElement.
The function:
- Normalizes props by extracting
keyandref - Collects all children arguments (supports variadic arguments)
- Calls internal
createVNodeto 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 useh:
TypeScript (tsconfig.json)
Related APIs
createElement- Identical function with different nameFragment- Render multiple children without a wrappercloneElement- Clone and modify existing VNodes