React on Rails Client-Side Integration

16.6.0 · active · verified Wed Apr 22

react-on-rails is the client-side JavaScript package designed to integrate React applications seamlessly within a Ruby on Rails environment, specifically leveraging the Rails asset pipeline (e.g., Shakapacker, Webpack, Rspack). Currently at stable version 16.6.0, the library facilitates component registration, client-side hydration, and server-side rendering (SSR) of React components. It maintains an active release cadence with frequent updates and bug fixes, indicated by its minor and patch releases. Its primary differentiator is its tight coupling with the `react_on_rails` Ruby gem, providing a robust, opinionated solution for isomorphic React development in Rails. This includes essential functionalities like connecting React components to Redux stores and supporting advanced features such as server-side rendering, with specific capabilities for TanStack Router via its `react-on-rails-pro` offering. It's a foundational component for modern React-in-Rails development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart registers a simple 'HelloWorld' React component with `react-on-rails`, making it available for rendering within Rails views using the `react_component` helper, supporting both client-side hydration and server-side rendering.

import React from 'react';
import ReactOnRails from 'react-on-rails';

interface HelloWorldProps {
  name: string;
}

const HelloWorld: React.FC<HelloWorldProps> = ({ name }) => {
  return (
    <div>
      <h1>Hello from React, {name}!</h1>
      <p>This component is registered with React on Rails.</p>
    </div>
  );
};

// This line registers the component for client-side and server-side rendering.
// It makes 'HelloWorld' available to the `react_component` Rails helper.
ReactOnRails.register({ HelloWorld });

console.log('HelloWorld component registered successfully.');

view raw JSON →