Miss any of our Open RFC calls?Watch the recordings here! »


2.15.0 • Public • Published


Element is, quite simply, an abstraction layer atop React.

You may find yourself asking, "Why an abstraction layer?". For a few reasons:

  • In many applications, especially those extended by a rich plugin ecosystem as is the case with WordPress, it's wise to create interfaces to underlying third-party code. The thinking is that if ever a need arises to change or even replace the underlying implementation, it can be done without catastrophic rippling effects to dependent code, so long as the interface stays the same.
  • It provides a mechanism to shield implementers by omitting features with uncertain futures (createClass, PropTypes).
  • It helps avoid incompatibilities between versions by ensuring that every plugin operates on a single centralized version of the code.

On the wp.element global object, you will find the following, ordered roughly by the likelihood you'll encounter it in your code:


Install the module

npm install @wordpress/element --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.


Let's render a customized greeting into an empty element:

<div id="greeting"></div>
function Greeting( props ) {
    return wp.element.createElement( 'span', null, 
        'Hello ' + props.toWhom + '!'
    wp.element.createElement( Greeting, { toWhom: 'World' } ),
    document.getElementById( 'greeting' )

Refer to the official React Quick Start guide for a more thorough walkthrough, in most cases substituting React and ReactDOM with wp.element in code examples.

Why React?

At the risk of igniting debate surrounding any single "best" front-end framework, the choice to use any tool should be motivated specifically to serve the requirements of the system. In modeling the concept of a block, we observe the following technical requirements:

  • An understanding of a block in terms of its underlying values (in the random image example, a category)
  • A means to describe the UI of a block given these values

At its most basic, React provides a simple input / output mechanism. Given a set of inputs ("props"), a developer describes the output to be shown on the page. This is most elegantly observed in its function components. React serves the role of reconciling the desired output with the current state of the page.

The offerings of any framework necessarily become more complex as these requirements increase; many front-end frameworks prescribe ideas around page routing, retrieving and updating data, and managing layout. React is not immune to this, but the introduced complexity is rarely caused by React itself, but instead managing an arrangement of supporting tools. By moving these concerns out of sight to the internals of the system (WordPress core code), we can minimize the responsibilities of plugin authors to a small, clear set of touch points.


While not at all a requirement to use React, JSX is a recommended syntax extension to compose elements more expressively. Through a build process, JSX is converted back to the createElement syntax you see earlier in this document.

If you've configured Babel for your project, you can opt in to JSX syntax by specifying the pragma option of the transform-react-jsx plugin in your .babelrc configuration.

    "plugins": [
        [ "transform-react-jsx", {
            "pragma": "createElement"
        } ]

This assumes that you will import the createElement function in any file where you use JSX. Alternatively, consider using the @wordpress/babel-plugin-import-jsx-pragma Babel plugin to automate the import of this function.


# Children

Object that provides utilities for dealing with React children.

# cloneElement

Creates a copy of an element with extended props.


  • element WPElement: Element
  • props ?Object: Props to apply to cloned element


  • WPElement: Cloned element.

# Component

A base class to create WordPress Components (Refs, state and lifecycle hooks)

# concatChildren

Concatenate two or more React children objects.


  • childrenArguments ...?Object: Array of children arguments (array of arrays/strings/objects) to concatenate.


  • Array: The concatenated value.

# createContext

Creates a context object containing two components: a provider and consumer.


  • defaultValue Object: A default data stored in the context.


  • Object: Context object.

# createElement

Returns a new element of given type. Type can be either a string tag name or another function which itself returns an element.


  • type ?(string|Function): Tag name or element creator
  • props Object: Element properties, either attribute set to apply to DOM node or values to pass through to element creator
  • children ...WPElement: Descendant elements


  • WPElement: Element.

# createInterpolateElement

This function creates an interpolated element from a passed in string with specific tags matching how the string should be converted to an element via the conversion map value.


For example, for the given string:

"This is a string with a link and a self-closing tag"

You would have something like this as the conversionMap value:

    span: <span />,
    a: <a href={ 'https://github.com' } />,
    CustomComponentB: <CustomComponent />,


  • interpolatedString string: The interpolation string to be parsed.
  • conversionMap Object: The map used to convert the string to a react element.


  • WPElement: A wp element.

# createPortal

Creates a portal into which a component can be rendered.



  • child (unknown type): Any renderable child, such as an element, string, or fragment.
  • container HTMLElement: DOM node into which element should be rendered.

# createRef

Returns an object tracking a reference to a rendered element via its current property as either a DOMElement or Element, dependent upon the type of element rendered with the ref attribute.


  • Object: Ref object.

# findDOMNode

Finds the dom node of a React component.


  • component (unknown type): Component's instance.

# forwardRef

Component enhancer used to enable passing a ref to its wrapped component. Pass a function argument which receives props and ref as its arguments, returning an element using the forwarded ref. The return value is a new component which forwards its ref.


  • forwarder Function: Function passed props and ref, expected to return an element.


  • WPComponent: Enhanced component.

# Fragment

A component which renders its children without any wrapping element.

# isEmptyElement

Checks if the provided WP element is empty.


  • element *: WP element to check.


  • boolean: True when an element is considered empty.

# isValidElement

Checks if an object is a valid WPElement.


  • objectToCheck Object: The object to be checked.


  • boolean: true if objectToTest is a valid WPElement and false otherwise.

# lazy


# memo


# Platform

Component used to detect the current Platform being used. Use Platform.OS === 'web' to detect if running on web enviroment.

This is the same concept as the React Native implementation.


Here is an example of how to use the select method:


import { Platform } from '@wordpress/element';
const placeholderLabel = Platform.select( {
  native: __( 'Add media' ),
  web: __( 'Drag images, upload new ones or select files from your library.' ),
} );


Component used as equivalent of Fragment with unescaped HTML, in cases where it is desirable to render dangerous HTML without needing a wrapper element. To preserve additional props, a div wrapper will be created if any props aside from children are passed.


  • props RawHTMLProps: Children should be a string of HTML. Other props will be passed through to div wrapper.


  • JSX.Element: Dangerously-rendering component.

# render

Renders a given element into the target DOM node.


  • element (unknown type): Element to render.
  • target HTMLElement: DOM node into which element should be rendered.

# renderToString

Serializes a React element to string.


  • element (unknown type): Element to serialize.
  • context [Object]: Context object.
  • legacyContext [Object]: Legacy context object.


  • string: Serialized element.

# StrictMode

Component that activates additional checks and warnings for its descendants.

# Suspense


# switchChildrenNodeName

Switches the nodeName of all the elements in the children object.


  • children ?Object: Children object.
  • nodeName string: Node name.


  • ?Object: The updated children object.

# unmountComponentAtNode

Removes any mounted element from the target DOM node.


  • target Element: DOM node in which element is to be removed

# useCallback


# useContext


# useDebugValue


# useEffect


# useImperativeHandle


# useLayoutEffect


# useMemo


# useReducer


# useRef


# useState


Code is Poetry.


npm i @wordpress/element

DownloadsWeekly Downloads






Unpacked Size

561 kB

Total Files


Last publish


  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar