docs/oss/getting-started/using-react-on-rails.md
This guide explains the core concepts of React on Rails and how everything fits together. If you've just installed React on Rails (or are about to), this will help you understand what's happening under the hood.
š” New to React on Rails? Start with the 15-Minute Quick Start to get something working first, then come back here to understand the concepts.
When you install React on Rails, several things happen:
The generator sets up:
app/javascript/bundles/ or with auto-bundling in app/javascript/src/*/ror_components/)For detailed installation instructions, see:
Once installed, you render React components in Rails views using the react_component helper:
<%= react_component("HelloWorld", props: @some_props) %>
Client-side rendering only (default):
<%= react_component("HelloWorld", props: { name: "World" }) %>
Server-side rendering for SEO/performance:
<%= react_component("HelloWorld", props: { name: "World" }, prerender: true) %>
The component name ("HelloWorld") must match the name you registered in your JavaScript code.
React on Rails is configured in config/initializers/react_on_rails.rb:
For complete configuration options, see the Configuration Reference.
For all view helper options (props, HTML options, tracing, etc.), see the View Helpers API.
React on Rails supports two approaches for making components available to Rails views:
// app/javascript/packs/hello-world-bundle.js
import ReactOnRails from 'react-on-rails';
import HelloWorld from '../components/HelloWorld';
ReactOnRails.register({ HelloWorld });
You must configure webpack entry points and manually register each component.
<%= react_component("HelloWorld", { name: "World" }, { auto_load_bundle: true }) %>
With auto-bundling enabled:
app/javascript/src/*/ror_components/)ReactOnRails.register() callsConfiguration (in config/initializers/react_on_rails.rb):
config.components_subdirectory = "ror_components" # Directory name for auto-discovery
config.auto_load_bundle = true # Enable automatic bundle loading
Benefits:
For complete details, see Auto-Bundling Guide.
After running rails generate react_on_rails:install, you'll see:
app/javascript/
āāā bundles/HelloWorld/ # or src/HelloWorld/ror_components/ with auto-bundling
āāā HelloWorld.jsx
app/controllers/hello_world_controller.rb - Example controllerapp/views/hello_world/index.html.erb - Shows react_component helper usageconfig/routes.rbconfig/initializers/react_on_rails.rb - Configurationconfig/shakapacker.ymlThe generator creates bin/dev for starting both:
Note: You need
overmindorforemaninstalled to runbin/dev. Install withbrew install overmind(macOS) orgem install foreman(globally). See the Quick Start Guide for detailed installation instructions.
Sometimes you need more than just a simple React component. Render-Functions let you:
const MyApp = (props, railsContext) => {
// Access Rails context
console.log(railsContext.pathname); // Current URL
console.log(railsContext.i18nLocale); // Current locale
// Return a React component
return () => <div>Hello from {railsContext.pathname}</div>;
};
export default MyApp;
For advanced server rendering (like routing + hydration state), you can return an object:
({
renderedHtml: {
componentHtml,
redirectLocation,
error,
},
});
Use with react_component_hash helper for multiple HTML strings (useful with React Helmet for meta tags).
If you also need to send extra data back to the browser for hydration, add clientProps:
({
renderedHtml: '<div>SSR HTML</div>',
clientProps: {
// Optional: merged into client hydration props on the Rails side
routerDehydratedState: { url: railsContext.location },
},
redirectLocation: { pathname: '/login', search: '' }, // Optional
routeError: null, // Optional
});
Think of it like this: renderedHtml is what the user sees now, and clientProps is a lunchbox of data React uses after the page loads.
For complete Render-Function details and examples, see the Render-Functions Guide.
ReactOnRails::Errorconfig/initializers/react_on_rails.rbFor troubleshooting common issues, see the Troubleshooting Guide.
Now that you understand the core concepts, here are recommended paths forward:
react_component optionsReady to build something? The Tutorial walks you through building a complete app with Redux, routing, and testing.