Skip to content

Creating a React-Powered Theme in Liferay 7.4

Let’s build a modern Liferay theme using React, ES6, and Sass. This guide assumes you have Node.js (v14+) and Java JDK (8+) installed.

Terminal window
npm install -g yo generator-liferay-theme gulp
Terminal window
yo liferay-theme
  • Select: “Liferay Theme” (not Classic)
  • Theme name: “my-react-theme”
  • Choose: “JavaScript” as the language
  • Select: “styled” as the base theme

Terminal window
cd my-react-theme
npm install react react-dom @clayui/core @liferay/frontend-js-react-web
{
"exclude": {
"nodePackages": ["react", "react-dom"]
},
"soy": {
"enabled": false
},
"webpack": {
"rules": [
{
"test": "\\.jsx?$",
"exclude": "node_modules",
"use": ["babel-loader"]
}
]
}
}
{
"presets": ["@babel/preset-react", "@babel/preset-env"]
}

Create a Component (src/js/components/Navbar.jsx)

Section titled “Create a Component (src/js/components/Navbar.jsx)”
import React from "react";
import { ClayVerticalNav } from "@clayui/nav";
export default function Navbar() {
const items = [
{ label: "Home", href: "/" },
{ label: "About", href: "/about" },
{ label: "Contact", href: "/contact" },
];
return (
<div className="navbar-container">
<ClayVerticalNav items={items} />
</div>
);
}
import React from "react";
import { render } from "react-dom";
import Navbar from "./components/Navbar";
// Render to a DOM element with ID "navbar-root"
if (document.getElementById("navbar-root")) {
render(<Navbar />, document.getElementById("navbar-root"));
}

Modify the Template (src/templates/portal_normal.ftl)

Section titled “Modify the Template (src/templates/portal_normal.ftl)”

Add a mounting point for React:

<div id="navbar-root"></div>
<script src="${javascript_folder}/main.js" type="module"></script>

Style the Component (src/css/_custom.scss)

Section titled “Style the Component (src/css/_custom.scss)”
.navbar-container {
background: $light;
padding: 1rem;
margin-bottom: 2rem;
@include media-breakpoint-down(sm) {
padding: 0.5rem;
}
}

Terminal window
gulp watch
Terminal window
gulp build
Terminal window
gulp deploy

  1. Go to Control Panel → Configuration → Instance Settings
  2. Select your theme under “Look and Feel”
  3. Visit any page to see your React-powered navbar!

React works seamlessly with Liferay 7.4+ themes
Clay UI components provide Liferay-consistent styling
Webpack + Babel handle modern JS/JSX compilation
gulp watch enables hot-reloading for faster development


Add a React portlet to your theme
Use Liferay’s Headless APIs with fetch
Implement theme contributions for modular overrides