How to use React JS with Magento 2

Combine the power of a React JS front-end library with the internet’s most popular e-commerce CMS Magento 2

Want the advantages of a modern React JS, but need a back-end that feels familiar? In this article, we’ll go through how to use React with Magento 2 framework.

Magento 2 React JS without PWA theme.

The team was recently working on a Magento 2 module warranty/up-sell and checkout for a client when they sprung this question on me: ‘Can we use a modern front-end approach — React JS library?

Since late 2016, the answer to this question has been yes (React.Js 15 was released In April 2016). But the steps necessary to create a working Magento module may not seem straightforward, especially to those who aren’t familiar with both Magento 2 templates and React.

By default, Magento 2 using a mix of KnockoutJS and JQuery Widgets as UI Components.

We built this Magento 2 module. It integrates React, and WebPack builds a library with Magento infrastructure:

This implementation is a Hybrid React integration with Magento 2 (with Magento 1 also easy to use). It uses inline JSON directly from the page. The same approach is used in Magento 2 backend and frontend checkout, color swatches by default. Also, the Ajax HTTP call can be used to fetch data (not the best solution Magento API is slow and will increase the load on your back-end server). Or you can use my future project, “Microservices Magento,” to fetch data. Our most straightforward Magento 2 module uses WebPack for JSX React Components Compilation and automatic static content deployment into the Magento pub/static folder.

To fetch Magento Data directly from the database using modern ORM (Laravel/Eloquent), you can use this Magento-less microservice PHP library:

You can also use NodeJS to fetch data from Magento instead of PHP.

NodeGento(NodeJento) is a Magento-less microservice framework. It works with Magento 2 data directly without relying on legacy M2 slowcore code:

Magento components as a Microfrontend

The other approach uses a stand-along react library and embed functionality to the admin/front-end page via Iframe.

The other most straightforward approach to composing applications together in the browser is Iframe. Iframes make it easy to build a page out of independent sub-pages/components. They also offer a reasonable degree of isolation of styling and JS. Magento and ReactJS code not interfering with each other.

As an example, you can see this Magento OPcahe GUI ReactJS UI embedded as iframe implementation :

Update 2020:

After several discussions with the Magento and San Diego React community, we have realized a new version of the Magento 2 React.Js integration without the PWA Studio redundancy and compilation(complication) step using a stand-alone babel script. It can slow down the script execution because of real-time JSX transformations. Then use Compilation. However, for development purposes, it is ok. The performance of React JS is a different story.

Everything that you need to do is to add this code:

// Include React JS itself 
<script src=''></script>
// Include Babel to avoid compilation
<script src=""></script>

//Add to the page to render React component
<div id = "magentoReactApp"> </div>

//Write your scripts using babel
<script type="text/babel">

var App = React.createClass({
render: function() {
<div className="App">
{/*Your React Magento UI Component code there */}
<App />,

What is Babel for Magento?

Babel is a JavaScript compiler Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backward-compatible version of JavaScript in current and older browsers or environments. Here are the main things Babel can do for you:

Transform React JSX syntax:

  • Polyfill features that are missing in your target environment (through @babel/polyfill)
  • Source code transformations (codemods)

JSX and React with Magento 1,2

JSX is an XML-like syntax extension to ECMAScript without any defined semantics. It’s NOT intended to be implemented by engines or browsers. It’s NOT a proposal to incorporate JSX into the ECMAScript spec itself. It’s designed to be used by various preprocessors (transpilers) to transform this syntax into standard ECMAScript. In our case transpiler is Babel. Babel can convert JSX syntax!

Use Bable for Magento via UNPKG: This is a simple way to embed it on a webpage without having to do any other setup.

When loaded in a browser, @babel/standalone will automatically compile and execute all Magento's script tags with type ‘text/babel’ or ‘text/jsx’. React Equivalent to the Magento's ‘text/x-magento-init


<script type="text/babel" src="components.js"></script><script type="text/jsx">

var magento => () {
return <div className="App"><h1>Magento React JSX Component</h1></div>;


Video how to use Stand Alone Babel:

Sorry, guys, it is on Franch ;)

This video, describes Babel Standalone! A package that allows you to embed Babel in any application (including Magento 1/2) without a web pack and compilation!

This approach is fine for learning and creating simple demos. However, it makes your website slow and isn’t suitable for production.

That’s right, your code is not optimized, and loading Babel can be too heavy.

Remember, it’s for just for fun. There is another (and probably better) way with HTM (Hyperscript Tagged Markup), a JSX alternative using standard tagged templates made by @_developit , creator of Preact.

The download weight difference is enormous if we don’t need Babel.

LibVersionLoading SizeBabel6.26 < 195KB

HTM2.2.1 > 1KB

<script src="" crossorigin></script>
<script type="module">
// Bind htm with createElement
const html = htm.bind(createElement);

It’s like JSX but also lit

The syntax you write when using HTM is as close as possible to JSX:

  • Spread props: <div ...${props}>
  • Self-closing tags: <div />
  • Components: <${Foo}> (where Foo is a component reference)
  • Boolean attributes: <div draggable />

Improvements over JSX

htm takes the JSX-style syntax a couple steps further!

Here are some ergonomic features you get for free that aren’t present in JSX:

  • No transpiler necessary
  • HTML’s optional quotes: <div class=foo>
  • Component end-tags: <${Footer}>footer content<//>
  • Syntax highlighting and language support via the [lit-html VSCode extension] and [vim-jsx-pretty plugin].
  • Multiple root elements (fragments): <div /><div />
  • Support for HTML-style comments: <div><!-- comment --></div>

Syntax differences

There are few syntax differences by using the tag function html

// JSX syntax
function Header({title}) { return <h1>${title}</h1>}
function App() {
const name="World"
return (
<Header title="Hello, ${name}!"/>
// HTM syntax
function Header({title}) { return html`<h1>${title}</h1>`}
function App() {
const name="World"
return html`
<${Header} title="Hello, ${name}!"/>

Typical Ecommerce React Components

  • Main Menu
  • Responsive grids with pagination, filtering, and sorting
  • Media carousel with zoom
  • Color Selector
  • Size Selector
  • Quantity Selector
  • Search
  • Expandable Sections and Accordion for displaying product information
  • Add to Cart Button
  • Store Locator
  • Forms / Appointment / Contact

Magento/APP Cloud Architect. Melting metal server infrastructure into cloud solutions.