Client-Side Routing

If you want to use client-side routing with ReactPress, you need to make sure, that client-side routing doesn’t collide with the server-side routing of your WordPress installation.

Client-side routing without clean URLs

The easiest way to achieve this is to use router mechanisms that don’t send the URL to the server. React-Router provides <HashRouter>, <HistoryRouter> and <MemoryRouter> for these situations, which use can use instead of <BrowserRouter>.

These routers are especially useful, if you need routing and want to use your React app in more than one page. On the downside, they don’t provide clean URLs.

Using these routers has different trade-offs. Refer to the React Router documentation for more information on which router component is best for you.

Client-side routing with clean URLs

ReactPress provides the ability to use <BrowserRouter> with clean URLs. <BrowserRouter> lets you store the current location in the browser’s address bar using clean URLs and to navigate using the browser’s built-in history stack.

Using client-side routing with clean URLs has one caveat: The WordPress page of the React app you are using can’t have any subfolders. If you have a page URL: [your-domain]/parent. You can’t reach a page at [your-domain]/parent/children. As it would be caught by React-Router.

Set up your app in ReactPress

That information out of the way, let’s create a new React app with React-Router. This assumes that you have a running local WordPress dev system and ReactPress already installed. Change to the apps directory of ReactPress and create your React app. You find the path to the apps directory in the ReactPress admin page.

cd [path-to-your-app-directory]
npx create-react-app [your-appname]
cd [your-appname]
yarn add react-router-dom

Go back to the ReactPress admin, reload the page, and you should see your React app.

Add a page slug for your app (this may take a while) and check the “Use client-side routing” option. Now we can develop our React app.

Configure the React app

Open the src/index.js of your React app in your text editor. Import BrowserRouter from react-router-dom and wrap your app with <BrowserRouter basename={'/[slug-to-your-app-page]'}>.

// index.js
import React from 'react'
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import './index.css'
import App from './App'
import reportWebVitals from './reportWebVitals'

const root = ReactDOM.createRoot(document.getElementById('root'))
    <BrowserRouter basename={'/demo'}>
      <App />

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more:

Now you can use React Router anywhere in your app! But what does the basename property actually do? basename sets the base URL for all locations. If your app is served from a sub-directory (your WordPress page) on your server, you’ll want to set this to the sub-directory. A properly formatted basename should have a leading slash, but no trailing slash.

Build a simple React app

For a simple example, open src/App.js and replace the default markup with some routes:

import * as React from "react";
import { Routes, Route, Link } from "react-router-dom";
import "./App.css";

export default function App() {
  return (
    <div className="App">
      <h1>Welcome to ReactPress with React Router!</h1>
        <Route path="/" element={<Home />} />
        <Route path="about" element={<About />} />

Now, still in src/App.js, create your route components:

// App.js


function Home() {
  return (
        <h2>Welcome to the homepage!</h2>
        <p>You can do this, I believe in you.</p>
        <Link to="/about">About</Link>

function About() {
  return (
        <h2>Who are we?</h2>
          That feels like an existential question, don't you
        <Link to="/">Home</Link>

Go ahead and start your app by running npm start, go to http://localhost:3000/[slug-to-your-app-page]/ and you should see the Home route when your app starts running. Click the “About” link to see your <About> route, and voilà! You’ve successfully set up React Router within ReactPress 🥳

Now build your React app with yarn build and visit the page of your React app in your development system’s WordPress.

This tutorial is based on React-Router’s Installation page CC 4.0