How to Use @wordpress/element in React

Create Ticket

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.

Featurereact + 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

Leave a Reply

Your email address will not be published. Required fields are marked *

About Company

CodersGrow is specialize in crafting high-quality Laravel and WordPress plugins, Node Applications themes, and custom web solutions that drive results.

IT SERVICES

Web Developement

E-commerce Development

Single Page Applications (SPAs)

WordPress CMS Development

MERN Development

Contact us

QUICK MAIL US:

sales@codersgrow.com

LOCATION:

C Block, Sector-22 , Noida 201301 Uttar Pradesh

ยฉ Copyright 2025, CodersGrow Technologies (Registered in India). All rights reserved.

CodersGrow Technologies doesn’t authorize any third party sellers to re-sell CodersGrow Technologies modules.