Introducing a new way to review your PRs.

Read the announcement

shakacode / react-webpack-rails-tutorial

Legacy full-app React on Rails tutorial repo and live demo. See reactonrails.com/examples for current maintained demos.

View on GitHub

16 documents · Updated Nov 4, 2026

docs / react-on-rails-integration

React on Rails integration

React on Rails connects Rails page rendering to named React components and stores. Rails templates choose the component and its input, while the React bundles provide the browser and server rendering behavior. This page focuses on that boundary; see React rendering modes for the larger choice between browser rendering, server rendering, and React Server Components.

Integration model

The integration has three cooperating parts: Rails configuration sets the rendering rules, view helpers place React output in an HTML response, and bundle registration maps names such as App to React code. The same page can therefore receive server-rendered HTML first and then use the matching client bundle to make that HTML interactive.

The main hand-offs are:

StageResponsibility
Rails initializerSelects bundle locations, default rendering behavior, and the renderer used by Pro features.
Controller and viewSupplies props or store data and asks React on Rails to render a named component.
Registered bundleResolves the component name and creates the React tree for the current page.
Shared contextCarries request information into component and store factories so server and browser work from the same page state.

The request path is summarized below.

flowchart LR
  Request[Browser request] --> Controller[Rails controller]
  Controller --> View[Rails template and layout]
  View --> Helper[React on Rails view helper]
  Helper --> Props[Props and store hydration data]
  Helper --> Context[Shared Rails context]
  Helper --> Registry[Registered React component]
  Registry --> HTML[Server HTML or client mount point]
  HTML --> Browser[Browser bundle hydration]

Rails configuration

The Rails initializers define the contract between Rails and the React on Rails runtime. config/…/react_on_rails.rb sets the component discovery area, enables bundle loading, turns on server rendering by default, and names the server bundle. config/…/react_on_rails_pro.rb selects the Pro Node renderer and enables the RSC route support used by the wider rendering examples.

Core rendering settings

The core initializer makes server rendering the default for component helpers, while individual views can override it. Development tracing and bundle reload behavior follow the Rails environment. In production, rendering errors do not fail the whole Rails page by default; in other environments they are raised to expose integration errors early.

The initializer also points test asset lookup at the Shakapacker output used by this application. This keeps Rails component lookup aligned with the bundle build rather than with a different default asset directory.

References: config/…/react_on_rails.rb• L4-6 config/…/react_on_rails.rb• L19-23 config/…/react_on_rails.rb• L36-38 config/…/react_on_rails.rb• L46-72

Pro renderer settings

The Pro initializer moves server rendering to a separate Node service. Rails calls that service through the configured renderer URL and shared password. It also enables the server component support that exposes the RSC payload route; the Node boundary and its process belong to Node server rendering, while the page behavior belongs to React Server Components.

References: config/…/react_on_rails_pro.rb• L7-14 config/…/react_on_rails_pro.rb• L21-29 config/routes.rb• L3-4

Rails page entry points

Rails templates are the page entry points for React on Rails. A template names a registered component and can pass props, choose server rendering, or rely on the application-wide defaults. Controllers prepare data before rendering when the component needs records or a Redux store.

Component helper pages

The ordinary react_component helper covers the main integration path. The home page mounts RouterApp, the no-router page mounts App, and the simple page mounts SimpleCommentScreen without server rendering. The ReScript page uses the same helper contract, so the source language of a component does not change how Rails places it on the page.

These pages show two input patterns. A view can pass props directly, as the server-components page does, or a controller can register a named Redux store before the view renders, as the index and no-router actions do.

References: app/…/index.html.erb• L200-205 app/…/no_router.html.erb• L1-6 app/…/simple.html.erb• L4-16 app/…/rescript.html.erb• L1 app/…/pages_controller.rb• L8-40

Layout component pages

The application layouts use the same helper for shared navigation and footer components. This puts common React UI around the Rails view body instead of making each page repeat those mounts. The main layout also emits Redux hydration data after the shared components so browser stores can reuse the state prepared during the Rails request.

The Stimulus-oriented layout keeps the shared component mounts but does not emit Redux hydration data. Its pages therefore use the layout as a common React shell without the store hydration contract used by the comment pages.

References: app/…/application.html.erb• L1-12 app/…/stimulus_layout.html.erb• L1-9

Streamed component pages

The server-components page uses stream_react_component instead of the ordinary helper. The controller supplies a limited comment set and asks React on Rails Pro to stream the component output into the response. This is a separate page entry path for RSC work, not a replacement for the regular component helper.

References: app/…/pages_controller.rb• L42-46 app/…/server_components.html.erb• L1-5

Component registration

Registration creates the name-to-component map that Rails helpers use. The repository keeps top-level React on Rails components in ror_components directories and enables automatic loading for them. A separate registration module explicitly lists the components and stores needed by the server rendering bundle.

Component discovery

The component subdirectory setting establishes ror_components as the discovery convention. The client bundle follows that convention for App, RouterApp, NavigationBarApp, SimpleCommentScreen, Footer, and the ReScript wrapper. Rails can then request a component by its public name without knowing its source path.

The server registration module supplies the same public names to React on Rails and registers the two Redux store factories. This keeps the server side able to build the same page tree that the browser will later hydrate.

References: config/…/react_on_rails.rb• L4-8 client/…/serverRegistration.jsx• L2-21 client/…/App.jsx• L1-25 client/…/RouterApp.server.jsx• L1-41

Store registration

Redux stores are registered separately because a store is a factory for page state rather than a React component. The stores-registration pack registers routerCommentsStore and commentsStore for the browser. The server registration module registers the same names for server rendering, so both sides can consume the data that Rails supplies.

The application layout loads the store-registration pack with the shared client packs. This makes the stores available before mounted components ask React on Rails for them.

References: client/…/stores-registration.js• L1-12 client/…/serverRegistration.jsx• L8-21 app/…/application.html.erb• L21-25

Shared rendering context

React on Rails supplies a Rails context to component and store factories for each request. The context links the Rails request to the React tree, which lets server and browser entry points make page-aware choices without duplicating routing data.

Request context

The router component reads location from the server context and gives it to StaticRouter. Its browser counterpart uses BrowserRouter for later navigation. The navigation component reads pathname from the same context to choose the correct store or render a plain navigation bar on pages that do not use Redux.

This arrangement keeps the Rails URL as the source of truth for the first render. The server tree and the browser tree can then agree on the route before client navigation takes over.

References: client/…/RouterApp.server.jsx• L10-37 client/…/RouterApp.client.jsx• L10-18 client/…/NavigationBarApp.jsx• L19-41

Store hydration

The controller serializes comment data into a named store before rendering the page. The store factory combines those comments with the Rails context, and the layout emits hydration data so the browser can reuse the server-prepared store instead of starting from unrelated state.

The router and no-router pages use different store names because their React trees have different routing needs. Their component factories retrieve the matching store by name, which makes the controller, registration module, layout, and component tree one shared contract.

References: app/…/pages_controller.rb• L28-35 app/…/application.html.erb• L10-12 client/…/commentsStore.js• L8-22 client/…/routerCommentsStore.js• L11-31 client/…/App.jsx• L13-21

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the component names in Rails templates aligned with the registration map and the ror_components discovery convention.
  • Keep each Redux store name aligned across controller setup, browser registration, server registration, component lookup, and layout hydration.
  • If the default server bundle or renderer settings change, check both ordinary react_component pages and the streamed server-components entry point.
  • If a layout changes its loaded packs, confirm that shared components and browser store registration remain available before hydration.
  • If route names or path handling change, check the Rails context consumers that select stores or configure router location.

Important failure modes

TriggerConsequenceGuard
A template requests a name that is absent from discovery or registrationReact on Rails cannot resolve the component for the pageKeep public names in templates and registration modules in sync.
A controller uses a store name that differs from the component lookup nameThe component receives no matching hydrated storeTreat the store name as a shared contract across Rails and both bundles.
The layout omits redux_store_hydration_data while a page expects server-prepared Redux stateThe browser cannot hydrate the store from the Rails responseKeep the hydration helper in layouts that host hydrated Redux components.
Server and browser router entry points use different request locationsThe first browser render can disagree with the HTML from RailsPass the Rails context to the server router and preserve the matching client entry point.
The Rails asset directory does not match the Shakapacker outputBundle lookup fails even though the assets were builtKeep generated_assets_dir and the active pack output path aligned, especially in tests.
The Node renderer is unavailable while ExecJS fallback is disabledServer-rendered pages fail instead of silently changing rendering modeStart the configured renderer and keep its URL and password paired with the Rails initializer.

Key files

FileWhy to read it
config/…/react_on_rails.rbDefines the core React on Rails defaults and bundle lookup rules.
config/…/react_on_rails_pro.rbDefines the Pro renderer boundary and RSC support settings.
app/…/pages_controller.rbShows how Rails prepares props, stores, and streamed component requests.
app/views/layouts/application.html.erbShows shared component mounts, pack loading, and store hydration.
app/views/pages/index.html.erbShows a routed React entry point inside a Rails page.
app/views/pages/simple.html.erbShows a minimal client-only component entry point.
app/views/pages/server_components.html.erbShows the streamed component entry point.
client/…/serverRegistration.jsxShows the public component and store registration contract.
client/…/stores-registration.jsShows browser-side store registration.
client/…/RouterApp.server.jsxShows how server routing consumes the Rails context.
client/…/NavigationBarApp.jsxShows a shared component choosing behavior from the page context.
client/…/commentsStore.jsShows how Rails props and context become browser/server store state.
client/…/routerCommentsStore.jsShows the routed variant of the shared store contract.