If youβre wondering how to use @wordpress/element in WordPress, youβre in the right place. WordPress already ships with its own React abstraction, @wordpress/element, which lets you build powerful React-based themes and plugins without installing React or ReactDOM separately. In this guide, weβll walk through a complete setup using Webpack and Babel to create a lightweight React frontend thatβs fully compatible with WordPress.
When building React features, developers often install React via npm β but that can cause version conflicts. Instead, learning how to use @wordpress/element in WordPress ensures your code stays lightweight, compatible, and aligned with WordPress core.
Installing React and ReactDOM as npm dependencies makes your project heavier and can even cause version conflicts with the React that WordPress already ships.
But hereβs the good news: WordPress already includes React under the hood via its own wrapper library called @wordpress/element.
By using @wordpress/element, you can:
β
Reduce dependencies
β
Prevent React conflicts
β
Ensure compatibility with Gutenberg and the WordPress ecosystem
In this guide, weβll build a simple React-powered frontend inside a WordPress theme using @wordpress/element, Webpack, and Babel β without installing React separately.
π§ What Is @wordpress/element and How to Use It in WordPress?
Normally, youβd install React like this:
npm install react react-dom
But in WordPress development, thatβs unnecessary (and risky).
Instead, you use @wordpress/element, which is just a WordPress abstraction layer around React and ReactDOM. It uses the core React instance already bundled with WordPress.
| Feature | react + react-dom (npm) | @wordpress/element |
|---|---|---|
| Redundant React installs | β Risk | β Avoided |
| Native to WP | β | β Yes |
| Optimized for Gutenberg | β | β Yes |
| Plugin/theme friendly | π¬ | β Compatible |
π Folder Structure for Using @wordpress/element in WordPress
Hereβs what our WordPress theme will look like:
/my-theme/ βββ assets/ β βββ js/ β βββ index.js βββ dist/ β βββ bundle.js βββ functions.php βββ header.php βββ footer.php βββ package.json βββ webpack.config.js
π§° Step 1: Install Packages to Use @wordpress/element in WordPress
No need for React or ReactDOM. Just install @wordpress/element and build tools.
npm init -y npm install @wordpress/element npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env @babel/preset-react
π¦ Step 2: Babel Configuration
Create a .babelrc file in your theme root:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
This allows us to use modern JavaScript (ES6+) and JSX.
βοΈ Step 3: Webpack Config
Create webpack.config.js:
const path = require('path');
module.exports = {
entry: './assets/js/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader',
}
],
},
resolve: {
extensions: ['.js', '.jsx'],
},
externals: {
react: 'React',
'react-dom': 'ReactDOM',
},
mode: 'development'
};
π The externals section is key β it tells Webpack not to bundle React or ReactDOM, because WordPress will load them via @wordpress/element.
βοΈ Step 4: Usage in React
π§ Note: The /** @jsx createElement */ pragma tells Babel to use WordPressβ createElement instead of Reactβs.
/** @jsx createElement */
import { createElement, render } from '@wordpress/element';
const App = () => (
<div>
<h2>Hello from @wordpress/element + JSX π</h2>
</div>
);
window.addEventListener('DOMContentLoaded', () => {
const root = document.getElementById('react-root');
if (root) {
render(<App />, root);
}
});
π¨ Step 5: Build the App
Add this to your package.json :
"scripts": {
"build": "webpack"
}
Run:
npm run build
Your compiled app will appear in dist/bundle.js.
π― Step 6: Enqueue Script in WordPress
In functions.php:
function my_theme_enqueue_assets() {
// Load WP's React wrapper
wp_enqueue_script( 'wp-element' );
// Load your compiled app
wp_enqueue_script(
'my-react-app',
get_template_directory_uri() . '/dist/bundle.js',
array('wp-element'),
filemtime(get_template_directory() . '/dist/bundle.js'),
true
);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_assets');
This ensures WordPress loads its own React first, then your bundle.
π§© Step 7: Add a React Root
In your header.php (or anywhere you want the app to appear):
<div id="react-root"></div>
Now your React app will render inside that div.
β Done! Youβve Learned How to Use @wordpress/element in WordPress π
When you refresh your WordPress site, you should see:
Hello from @wordpress/element + JSX π
π§ Bonus: Using Hooks in WordPress React
Yes, you can use hooks like useState and useEffect β straight from @wordpress/element:
/** @jsx createElement */
import { createElement, render, useState } from '@wordpress/element';
const App = () => {
const [count, setCount] = useState(0);
return (
<div>
<p>Clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
};
window.addEventListener('DOMContentLoaded', () => {
const root = document.getElementById('react-root');
if (root) {
render(<App />, root);
}
});
π Why Use @wordpress/element?
- π« No duplicate React installations
- β Native to WordPress core
- β‘ Smaller bundles
- π§© Compatible with Gutenberg and custom blocks
Now you know exactly how to use @wordpress/element in WordPress to build React-powered apps inside themes and plugins. This approach keeps your bundles small, avoids dependency issues, and ensures compatibility with Gutenberg.
Itβs simply the cleanest way to build React features inside WordPress themes or plugins.
π Thatβs it! Youβve just built a React frontend inside WordPress without React NPM dependencies.
π Submit your request now and letβs start building your dream eCommerce store.
π§ Have questions before you start? Visit our Contact Page
π Browse our full range of services at codersgrow.com
ποΈ Check out our Store for ready-to-use solutions
π Explore our Blogs for insights, tips, and industry updates
