@wordpress/element package is a React abstraction layer that provides utilities to work with React components and elements. It re-exports React’s core functionality while adding WordPress-specific enhancements.
Installation
Install the package using npm:This package assumes that your code will run in an ES2015+ environment. If you’re using an environment with limited support for such language features, include the polyfill shipped in
@wordpress/babel-preset-default.Why React?
The choice to use React is motivated by the technical requirements of the block system:- Understanding a block in terms of its underlying values
- Describing the UI of a block given these values
Basic Usage
You can import React functionality directly from@wordpress/element:
Core React APIs
Component Creation
createElement
Create React elements from components or tags
Component
Base class for creating class components
Fragment
Group children without adding extra DOM nodes
forwardRef
Pass refs to child components
React Hooks
All standard React hooks are available:WordPress-Specific Utilities
createInterpolateElement
Create interpolated elements from strings with specific tags:RawHTML
Render unescaped HTML safely:The wrapper
div is removed during block saving unless non-children props are present.Platform
Detect the current platform (useful for cross-platform code):Working with Refs
createRef
Create a ref object:useRef Hook
For function components, use theuseRef hook:
Rendering Components
createRoot (React 18+)
Create a root to render your app:hydrateRoot
Hydrate server-rendered markup:Advanced APIs
createPortal
Render children into a different part of the DOM:Suspense and Lazy Loading
Lazy load components:flushSync
Force synchronous updates:Utility Functions
isValidElement
Check if an object is a valid React element:cloneElement
Clone an element with new props:Children Utilities
Work with children:StrictMode
Enable strict mode for development checks:Package Details
This package bundles React directly and requires no additional peer dependencies. It currently includes React 18.3.0.
Related Packages
@wordpress/components- UI components built with this package@wordpress/compose- Hooks and HOCs for React@wordpress/data- State management layer
External Resources
- React Documentation - Official React docs
- React Hooks Reference - Complete hooks API
- React Components Reference - Class components API