Skip to content
Anton edited this page Feb 26, 2020 · 16 revisions

These are the methods exported by Preact 8.

import { h, createElement,
  cloneElement, createRef, Component,
  render, rerender, options,
} from 'preact'

On This Page

There are special properties that can be passed to the Preact component. They are defined in the PreactProps type to prevent renaming by the compiler, but this type extends Object.

PreactProps

Name Type & Description
dangerouslySetInnerHTML { __html: * }
Sets HTML of the node without escaping the contents.
ref function(?)
The reference callback for the node.

Accepted children are _VNode_s and primitives, and arrays of them.

(VNode | string | boolean | number | undefined) AcceptedChild: A child node that can be passed to some methods.

h(
  nodeName: string|!Function,
  attributes=: PreactProps,
  ...args: AcceptedChild|Array<AcceptedChild>,
): !VNode

The pragma (rendering) function.

  • nodeName* (string | !Function): An element name. Ex: div, a, span, etc.
  • attributes PreactProps (optional): Any attributes/props to set on the created element.
  • ...args (AcceptedChild | Array<AcceptedChild>) (optional): Additional arguments are taken to be children to append. Can be infinitely nested Arrays.

Alias: createElement.

cloneElement(
  vnode: !VNode,
  props=: PreactProps,
  ...args: AcceptedChild|Array<AcceptedChild>,
): !VNode

Clones the given VNode, optionally adding attributes/props and replacing its children.

  • vnode* !VNode: The virtual DOM element to clone.
  • props PreactProps (optional): Attributes/props to add when cloning.
  • ...args (AcceptedChild | Array<AcceptedChild>) (optional): Any additional arguments will be used as replacement children.

createRef(): !Object

Just returns an object...?

render(
  vnode: !VNode,
  parent: Element,
  merge=: Element,
): void

Render JSX into a parent Element.

  • vnode* !VNode: A (JSX) VNode to render.
  • parent* Element: DOM element to render into.
  • merge Element (optional): Attempt to re-use an existing DOM tree rooted at merge.

rerender(): void

Rerenders all rendered elements.