Writing
React With a Rails Backend: The Current Options
React and Rails still pair well, but the tooling has changed. How to choose between Hotwire, jsbundling-rails, Vite Ruby, Inertia.js, react_on_rails and a separate React front end, and when server rendering matters.
By Ka Lun Chan · Web development / Architecture
How should you use React with a Rails backend now?
Start with what the pages need. If they’re mostly server-rendered with some interactivity, Rails’ default Hotwire stack may mean you don’t need React at all. For React islands in Rails views, use jsbundling-rails or Vite Ruby. For a single-page feel without building an API, use Inertia.js. Reach for the react_on_rails gem when you want server-rendered React components inside Rails, and a separate React app on a rails new --api backend when the front end is its own product.
I first wrote this on my old blog as two posts: why React and Rails make a good pair, and a step-by-step setup on Cloud9. This version keeps the argument that held up and replaces the tooling, most of which has changed.
Three things moved since then:
- Webpacker, the tool my setup relied on, has been retired. Rails now defaults to import maps and offers bundlers through jsbundling-rails.
- There are more good ways to connect the two, from Inertia.js to a fully separate front end.
- AWS Cloud9, the browser IDE I used for the walkthrough, is closed to new customers.
| Approach | When it fits | The catch |
|---|---|---|
| Hotwire (no React) | Mostly server-rendered pages that need some interactivity | No React components. Very rich client state gets awkward. |
| jsbundling-rails (esbuild or Bun) | A few React islands inside normal Rails views | Client-side only. No server rendering out of the box. |
| Vite Ruby | The same islands, with Vite’s dev server and fast reloads | One more process and config to own in development. |
| Inertia.js with Rails | A single-page app feel with Rails routing and controllers, no separate API | You commit to Inertia’s protocol. SSR adds a Node process. |
| react_on_rails gem | Rails views with server-rendered React components | Node SSR, streaming and Server Components are in the paid Pro tier. |
| Separate React app + rails new --api | A separate front-end team, or several clients sharing one API | Two deploys, CORS, cross-origin auth and an API contract to version. |
Why pair React with Rails at all?
Because each is good at the part the other finds awkward. React gives you reusable components and a predictable one-way flow of data for interactive UI, and Rails gives you routing, data access, background jobs and a fast path to a working API.
My original case was simple. A site built with a client-side JavaScript framework still needs a server-side application behind it, and Rails is a productive one. React lets you mix markup with JavaScript in components you can reuse across pages, and its one-way data binding is easier to reason about than the two-way binding that was common in frameworks at the time.
I also wrote that React’s virtual DOM made things faster. I’d qualify that now. React updates only what changed, which is fast enough for most apps, but plenty of UIs are just as fast with server-rendered HTML. I’d choose React today for its component model and ecosystem, and treat speed as something to measure.
The original also listed well-known companies using React. I’ve dropped that list. React’s adoption no longer needs proving, and the engineering write-ups it pointed to describe stacks from years ago.
Do you still need server-side rendering for SEO?
For public pages, it’s still the safer choice. Google renders JavaScript, but its own guidance still recommends server-side rendering or pre-rendering because it’s faster for users and crawlers, and not every bot runs JavaScript.
When I first wrote this, I cited Google’s announcement that it could render and understand pages built with JavaScript. That’s still true. Google’s JavaScript SEO basics describe crawling, then rendering in headless Chromium, then indexing the rendered HTML. The same page says server-side rendering or pre-rendering is still a great idea, and warns that some bots can’t run JavaScript at all.
The other half of my original argument was first paint. If Rails sends the above-the-fold content as HTML, visitors see the page before the browser has downloaded and run the JavaScript. That still holds. I wrote more about search and publishing in what publishing taught me about SEO, AEO and GEO.
So the deciding question is whether a page needs to rank. Logged-in dashboards rarely do, and client-side React is fine there. Marketing pages, articles and product listings usually do, and they should arrive as HTML.
What are the current ways to add React to Rails?
There are five realistic paths, plus Hotwire if you decide you don’t need React. They differ mostly in who owns routing and whether React renders on the server.
- Hotwire. Hotwire (Turbo and Stimulus) sends HTML over the wire instead of JSON, and the Rails guide calls it the default front-end stack in new Rails apps. Try it first when the interactivity is forms, lists and partial page updates.
- jsbundling-rails. The Rails JavaScript guide makes import maps the default, but says to use a bundler when your code needs a transpilation step such as JSX.
rails new -j esbuild(or Bun, Rollup or Webpack) sets that up, andbin/devruns Rails and the watcher together. - Vite Ruby. Vite Ruby wires Vite into Rails with
bundle exec vite installand runs its dev server withbin/vite dev. - Inertia.js. Inertia Rails lets a controller call
render inertia:with props, and a React page component receives them. There’s no client-side router and no separate API. SSR is optional and runs as a Node process. - react_on_rails. The react_on_rails gem is still maintained by ShakaCode, and it’s the closest descendant of my original setup. The open-source gem renders components from Rails views with server rendering. Node-rendered SSR, streaming SSR and React Server Components are in Pro, which is free in development but needs a paid license for production use unless your organization qualifies for free use.
- A separate front end.
rails new my_api --apigives you a slimmer API-only Rails app, and the front end is a Vite app or a framework such as Next.js, which react.dev now recommends for new React apps.
The separate front end gives the most freedom and costs the most to run. You own two deploys, CORS, authentication across origins and an API contract that both sides have to agree on. I covered those tradeoffs in API architecture lessons, and why I’d sketch the screens before designing that API in do you need a UI mockup before building the backend.
What does a minimal React on Rails setup look like today?
A new Rails app with esbuild, React installed with your package manager, one mount script, and a div in a Rails view that names the component and passes props as JSON. It replaces the Procfile, foreman and webpack configuration my original walkthrough needed.
rails new shop -j esbuild --skip-hotwire
cd shop
yarn add react react-dom # or npm/bun, matching the app's lockfile
bin/rails generate controller Pages home
bin/devThe esbuild build script that jsbundling-rails installs bundles every file at the top of app/javascript, so application.js imports the mount script and esbuild follows the imports from there:
// app/javascript/application.js
import "./react/mount.jsx";
// app/javascript/react/mount.jsx
import React from "react";
import { createRoot } from "react-dom/client";
import Greeting from "./Greeting.jsx";
const components = { Greeting };
document.querySelectorAll("[data-react]").forEach((el) => {
const Component = components[el.dataset.react];
const props = JSON.parse(el.dataset.props || "{}");
createRoot(el).render(<Component {...props} />);
});
// app/javascript/react/Greeting.jsx
import React from "react";
export default function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}<%# app/views/pages/home.html.erb %>
<%= tag.div data: { react: "Greeting", props: { name: "Rails" }.to_json } %>I skipped Hotwire here to keep the example small. If you keep Turbo, mount on turbo:load instead, because Turbo swaps pages without a full reload. This setup is client-side only. If the page needs to rank, move to react_on_rails or Inertia with SSR, or render the important content in ERB and let React enhance it. If you’re also moving an older app forward, what changed when upgrading Rails covers the rest of the stack.
Can you still follow the Cloud9 setup?
Not as a new user. AWS has closed Cloud9 to new customers. Existing customers can keep using it, but AWS says it isn’t adding new features and points people to its IDE Toolkits and CloudShell instead.
The original walkthrough ran everything in a Cloud9 workspace and edited the Procfile to bind Rails to the workspace’s IP and port. AWS’s migration guidance explains the closure and the alternatives.
For a React and Rails project today, the simplest path is local development: install Ruby and Node, run bin/dev, and open localhost:3000. If you want a browser-based environment, any hosted dev environment that can run Ruby and Node and forward a port will do. Check its port-forwarding settings, since that was the part of my Cloud9 setup that needed changes.
Sources
Checked in November against the Rails guides, the jsbundling-rails, react_on_rails and Webpacker repositories, the Inertia Rails and Vite Ruby docs, react.dev, Hotwire, Google’s JavaScript SEO guide and AWS’s Cloud9 documentation. The table, the example and the recommendations are mine.
- Ruby on Rails Guides: Working with JavaScript in Rails
- Ruby on Rails Guides: Using Rails for API-only Applications
- jsbundling-rails (GitHub)
- Webpacker has been retired (rails/webpacker on GitHub)
- React on Rails by ShakaCode (GitHub)
- Inertia Rails: Server-side setup
- Inertia Rails: Server-side rendering
- Vite Ruby: Getting started
- react.dev: Creating a React App
- Hotwire
- Google Search Central: Understand the JavaScript SEO basics
- AWS DevOps Blog: How to migrate from AWS Cloud9 to AWS IDE Toolkits or AWS CloudShell
- AWS Cloud9 User Guide: Document history
Short answers
What is the best way to use React with Rails?
It depends on the pages. For React islands inside Rails views, use jsbundling-rails or Vite Ruby. For a single-page app feel without a separate API, use Inertia.js. For server-rendered React components in Rails views, use the react_on_rails gem. When the front end is its own product, build it with Vite or Next.js against a Rails API created with rails new --api.
Do I need React if I use Rails?
Not always. Hotwire, made up of Turbo and Stimulus, is the default front-end stack in new Rails apps and covers forms, lists and partial page updates by sending HTML instead of JSON. Add React when screens need rich client-side state or you want its component ecosystem.
Does Google index client-side React pages?
Google renders JavaScript with headless Chromium before indexing, but its own guidance still recommends server-side rendering or pre-rendering, because it is faster for users and crawlers and not every bot runs JavaScript. Pages that need to rank should arrive as HTML.
Can I still use AWS Cloud9 for Rails development?
Only if you are an existing customer. AWS has closed Cloud9 to new customers and points people to its IDE Toolkits and CloudShell. Local development with bin/dev, or any hosted dev environment that runs Ruby and Node and forwards a port, works instead.
Choose the lightest setup that works
List the pages that need to rank and the screens that need rich client state, then choose the lightest row in the table that covers both. Most Rails apps need fewer moving parts than their first architecture diagram suggests.