React on Rails
React + Redux + React-Router on Rails Views via Webpack/NPM
including Server Rendering, v 6.1
by Justin Gordon, firstname.lastname@example.org
Presented at LA Ruby, November 10, 2016
Who is Justin?
• Palisades High, undergrad at Harvard, MBA from UC Berkeley
• Lived in Boston, then San Francisco, then telecommuting from Maui!
• Used to blog a lot at http://www.railsonmaui.com
• Started hiring and renamed my company to ShakaCode!
• We have our own app! https://www.friendsandguests.com
Task: Build an Interactive Dashboard
• Old “Rails Way”:
CoffeeScript and jQuery
• Client: madroneco.com
Why React Inside a Rails App?
• You need a rich client interface…You decide on React (smart)
• Rails only as an API server? Or all Node.js tooling?
• Nah! We ❤ Rails for both front and back-end development. In a
• That’s the sweet spot: the ability to mix and match simple Rails
screens with React where and when you need it, with ease!
• Use a React navigation header with a Rails content area.
Two videos =>Sold on React.js!
Pete Hunt: React: Rethinking best
practices -- JSConf EU 2013
Predictable, Not Correct. by Pete Hunt
How about the react-rails gem?
• react-rails is based on the standard asset pipeline inclusion of
• Simpler for newbies and those used to doing this the Rails way.
• Code organization is very much the Rails way, if you like that. (I’d
Why not the react-rails gem?
• The custom setup via the Rails asset pipeline is just not anything like
• How do you integrate Redux and React-Router?
• This is what led me to custom Webpack integration of Rails with React,
and eventually React on Rails!
• I just wanted to use an npm package called react-bootstrap, and there
was no obvious way to do that with react-rails in 2014.
• React-rails may support webpack someday; issues: #301 and #448.
Why create the React on Rails Gem?
• Server rendering?
• React on Rails Doctrine
with Ruby on Rails!
React on Rails
• React + Redux + React-Router + Webpack + Babel Rock!
• Both a Ruby Gem react_on_rails and a NPM Package react-on-
• Used by Friends and Guests, Pivotal Tracker, Blink Inc, and others,
• 2167+ ⭐ as of November 10, 2016
React on Rails
• All ﬁles for the client side under /client
• React: rendering
• Webpack + Babel + NPM: packages ﬁles for the browser (for distribution
via the Rails asset pipeline or a server for hot reloading assets)
• Favorite Libraries: Redux + React-Router: Key major JS components to use
with React, supported by React on Rails
• Tooling: ESLint, Flow, CSS-Modules, etc.: Many other cutting edge
not the Rails way!
React on Rails Integration with Rails
assets that are used by the asset pipeline, like any other JS and
• Development: Can either use statically created JS and CSS ﬁles,
or special view helpers can grab “hot-reloadable” versions of the
JS and CSS from a Webpack Development Server.
Adding React on Rails to a new Rails app
• Demo App and change one line and restart (no hot reloading)
• Show generated source in browser
• Code walkthrough in RubyMine
• Optional: show hot-reloading https://github.com/shakacode/react-
• Read The React on Rails Doctrine and React on Rails, 2000+ 🌟 Stars
• Lots of great docs at github.com/shakacode/react_on_rails
• Follow a simple tutorial
• Study example apps:
• Simple, no DB: test/demo app: github.com/shakacode/react_on_rails/tree/
• A more complex example at www.reactrails.com with code: https://
Want More Help?
• ShakaCode can build your app, or augment your engineering team. We’re
free to use techniques and code from our own app,
• ShakaCode offers consulting on React on Rails, from a modestly priced
starter pack of usage help to full app development. We’re experts at Rails/
• Check out forum.shakacode.com for discussions. We have a Slack room!
• Follow @railsonmaui for updates on React on Rails.
• Aloha from Justin, email@example.com, and the ShakaCode Team!
Tips for Newbies
• Do simplest things ﬁrst
• Initially skip:
• Server Rendering
• Hot Reloading
• CSS Modules
Hot Module Reloading in Rails
• Statically created assets always used for production and tests.
• Why HMR in development? No page refresh to see changes in JS and Sass code!
env_stylesheet_link_tag conﬁgure getting assets either from HMR server or
regular static ﬁles.
• Procfile.hot sets ENV value so Rails knows and starts HMR server for assets
on port 3500, client/server-rails-hot.js
• More details: Hot Reloading of Assets for Rails Development
React on a Rails View via React On Rails
• redux_store: Rails helper method, sets up a registered redux store,
either in controller or on the view, usable by react components,
initialized with props from Rails. If deﬁned on the view and server
rendering used, it must go before the react_component call.
• react_component: Rails view helper to put a registered component
on a Rails View. This component may talk to a registered redux store, or
it may create a redux store, getting initialized by props from Rails.
components (actually functions that generate them).
Client Side Rendering
HTML, using class matchers on hidden HTML elements, and does initialization, passing in props
from rails that is placed in a HTML data attribute.
1. Initialize any Redux Stores, via controller helper or view helper with props from Rails. Note
the use of <% and NOT <%= for the ERB call:
<% redux_store("SharedReduxStore", props: @app_props_server_render %>
2. Render any react components, optionally with props from Rails. Note the use of <%=
<%= react_component("HelloWorld", props: @app_props_server_render %>
• You don’t need Redux for React on Rails! It’s an option. You also don’t need the redux_store
API. You can initialize your redux stores with your components if you have a 1:1 mapping of a
redux store to a component. You do this with a “generator function” which is a function that takes
props and returns a Redux component attached to a store.
Server Side Rendering
• First, don’t try server rendering until client rendering is working without bugs!
• Set option prerender: true in the call to react_component
• React on Rails Server side rendering of React roughly does:
2. Create code to hydrate stores for the view
3. Create initialization code to server render react component, maybe passing props.
5. Returns a HTML string placed on the Rails view
Shared Redux Stores
Header React Component Body React Component Footer React Component
• Many React components communicating with the same redux store
• Supports server rendering and react-router
Shared Redux Stores
Header React Component
Body React Component Body Rails Partial
• Allows a header React component to be paired with either a body react
component or a body Rails partial.
• Header React component can be dynamic! Notiﬁcations indicators!
• Why use Rails views? Sometimes Rails is good enough! and WAY less code!
Header React Component
• react-router is supported including server rendering.
• If your server rendered router does a redirect, React on Rails
handles this by doing a redirect on the client side.
• More details are here.
techniques for Sass, images, etc. Advantage: simple
2. Webpack Generated CSS: More complex, but not hard to setup with our
1. CSS Modules. This is huge. Once you try this, you won’t go back. What is
it? Allows very simple CSS class names with small CSS ﬁles right next to
2. Hot Reloading. You can change the your Sass ﬁles, save, and then you’ll
see the browser update, without reloading. For conﬁguration of hot
reloading, see Hot Reloading of Assets For Rails Development.
• Fragment caching of Server Rendering: The fast way to generate
the HTML is to have it cached!
• React on Rails provides a test helper to optimize building the static
assets only when needed.
• This is a huge convenience!
• Be sure to setup conﬁg/initializers/react_on_rails.conﬁg
• Conﬁgure rails_helper:
RSpec.configure do |config|
# Ensure that if we are running js tests, we are using latest webpack assets
# This will use the defaults of :js and :server_rendering meta tags