SlideShare a Scribd company logo
React on Rails
React + Redux + React-Router on Rails Views via Webpack/NPM
including Server Rendering
github.com/shakacode/react_on_rails
1
Why?
• React + Redux + React-Router + Webpack + Babel Rock!
• Ruby on Rails Rocks!
• Let’s put them together!
• Both a Ruby Gem react_on_rails and a NPM Package react-on-rails!
• Used by Pivotal Tracker, Blink Inc, and others, listed here.
• 1200 ⭐ as of March 18, 2016
2
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
large app, only some screens need a high fidelity JavaScript UX.
• That’s the sweet spot: the ability to mix and match simple Rails
screens with React where and when you need it, with ease!
3
How about the react-rails gem?
• react-rails is based on the standard asset pipeline inclusion of
assets
• Benefits
• 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
rather run with the JavaScript herd for JavaScript!)
4
Why not the react-rails gem?
• The custom setup via the Rails asset pipeline is just not anything like
the native tooling popular in the JavaScript community.
• 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 react-bootstrap, and there was
no obvious way to do that with react-rails in 2014.
5
React on Rails: Key Parts
• All files for the client side under /client
• Webpack + Babel + NPM: packages files for the browser (for
distribution via the Rails asset pipeline or a server for hot reloading
assets)
• Redux + React-Router: Key major JS components to use with React,
supported by React on Rails
• ESLint, JSCS, Flow, CSS-Modules, etc.: Many other cutting edge
JavaScript technologies enabled by doing things the JavaScript way,
and not the Rails way!
6
React on Rails Integration with Rails
• Production and Tests: Webpack creates JavaScript and CSS
assets that are used by the asset pipeline, like any other JS and
CSS files.
• Development: Can either use statically created JS and CSS files,
or special view helpers can grab “hot-reloadable” versions of the
JS and CSS from a Webpack Development Server.
7
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!
• How?
• ReactOnRails view helpers, env_javascript_include_tag and
env_stylesheet_link_tag configure getting assets either from HMR server or
regular static files.
• 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
8
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 defined 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.
• ReactOnRails.register: JavaScript registration of stores and
components (actually functions that generate them).
9
Client Side Rendering
• React on Rails installs a post-load JavaScript function (turbolinks compatible) that scans the
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.
10
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:
1. Sets up the JavaScript “context” with your server side webpack build
2. Create code to hydrate stores for the view
3. Create initialization code to server render react component, maybe passing props.
4. Execute this JavaScript on the Rails Server as part of the rendering process
5. Returns a HTML string placed on the Rails view
11
Shared Redux Stores
AppReduxStore
Header React Component Body React Component Footer React Component
• Many React components communicating with the same redux store
• Supports server rendering and react-router
12
Shared Redux Stores
AppReduxStore
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! Notifications indicators!
• Why use Rails views? Sometimes Rails is good enough! and WAY less code!
Header React Component
AppReduxStore
13
react-router Integration
• 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.
14
CSS Options
1. Standard Rails: Just use React on Rails for JavaScript and your regular Rails
techniques for Sass, images, etc. Advantage: simple
2. Webpack Generated CSS: More complex, but not hard to setup with our
examples. Advantages:
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 files right next to
your JavaScript. Here’s an example in the react-webpack-rails-tutorial.
2. Hot Reloading. You can change the your Sass files, save, and then you’ll
see the browser update, without reloading. For configuration of hot
reloading, see Hot Reloading of Assets For Rails Development.
15
Performance
• Turbolinks: Don’t reload the JavaScript and CSS, just the HTML
• Fragment caching of Server Rendering: The fast way to generate
the HTML is to have it cached!
16
Running Tests
• 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 config/initializers/react_on_rails.config
• Configure 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

ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
17
Getting Started
• Read The React on Rails Doctrine
• 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/master/spec/dummy
• A more complex example at www.reactrails.com with code: https://
github.com/shakacode/react-webpack-rails-tutorial
18
Want Personalized Help?
• ShakaCode offers consulting on React on Rails, from a modestly priced
starter pack of usage help ($1500) to full app development. We’re experts
at Rails/React/Redux/React-Router/Webpack JavaScript apps!
• Check out forum.shakacode.com for code discussions.
• Ask Justin for a Slack invite.
• Get in touch with Justin: justin@shakacode.com.
• Follow @railsonmaui for updates on React on Rails.
• Aloha from Justin and the ShakaCode Team!
19

More Related Content

What's hot

Containers State of the Union
Containers State of the UnionContainers State of the Union
Containers State of the Union
Amazon Web Services
 
State of the Union: Containers on AWS
State of the Union: Containers on AWSState of the Union: Containers on AWS
State of the Union: Containers on AWS
Amazon Web Services
 
Building Global Serverless Backends
Building Global Serverless BackendsBuilding Global Serverless Backends
Building Global Serverless BackendsAmazon Web Services
 
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
Amazon Web Services
 
Amazon ECS Deep Dive
Amazon ECS Deep DiveAmazon ECS Deep Dive
Amazon ECS Deep Dive
Amazon Web Services
 
Real World Rails Deployment
Real World Rails DeploymentReal World Rails Deployment
Real World Rails Deployment
Alan Hecht
 
Scale Your Application while Improving Performance and Lowering Costs (SVC203...
Scale Your Application while Improving Performance and Lowering Costs (SVC203...Scale Your Application while Improving Performance and Lowering Costs (SVC203...
Scale Your Application while Improving Performance and Lowering Costs (SVC203...
Amazon Web Services
 
Automating Operations Workload
Automating Operations WorkloadAutomating Operations Workload
Automating Operations Workload
Amazon Web Services
 
Intro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETIntro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETAlan Hecht
 
Automated Compliance and Governance with AWS Config and AWS CloudTrail
Automated Compliance and Governance with AWS Config and AWS CloudTrailAutomated Compliance and Governance with AWS Config and AWS CloudTrail
Automated Compliance and Governance with AWS Config and AWS CloudTrail
Amazon Web Services
 
Docker on AWS
Docker on AWSDocker on AWS
Docker on AWS
Amazon Web Services
 
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech TalksSave 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
Amazon Web Services
 
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
Amazon Web Services
 
VMware and AWS together (June 2017)
VMware and AWS together (June 2017)VMware and AWS together (June 2017)
VMware and AWS together (June 2017)
Julien SIMON
 
Serverless Developer Experience
Serverless Developer ExperienceServerless Developer Experience
Serverless Developer Experience
Amazon Web Services
 
Deep Dive on Serverless Application Development
Deep Dive on Serverless Application DevelopmentDeep Dive on Serverless Application Development
Deep Dive on Serverless Application Development
Amazon Web Services
 
Running High Availability Websites with Acquia and AWS
Running High Availability Websites with Acquia and AWSRunning High Availability Websites with Acquia and AWS
Running High Availability Websites with Acquia and AWSAcquia
 
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
Amazon Web Services
 
Building faster websites: web performance with WordPress
Building faster websites: web performance with WordPressBuilding faster websites: web performance with WordPress
Building faster websites: web performance with WordPress
Johannes Siipola
 

What's hot (20)

Containers State of the Union
Containers State of the UnionContainers State of the Union
Containers State of the Union
 
State of the Union: Containers on AWS
State of the Union: Containers on AWSState of the Union: Containers on AWS
State of the Union: Containers on AWS
 
Building Global Serverless Backends
Building Global Serverless BackendsBuilding Global Serverless Backends
Building Global Serverless Backends
 
Deep Dive into AWS Fargate
Deep Dive into AWS FargateDeep Dive into AWS Fargate
Deep Dive into AWS Fargate
 
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
(DVO201) Scaling Your Web Applications with AWS Elastic Beanstalk
 
Amazon ECS Deep Dive
Amazon ECS Deep DiveAmazon ECS Deep Dive
Amazon ECS Deep Dive
 
Real World Rails Deployment
Real World Rails DeploymentReal World Rails Deployment
Real World Rails Deployment
 
Scale Your Application while Improving Performance and Lowering Costs (SVC203...
Scale Your Application while Improving Performance and Lowering Costs (SVC203...Scale Your Application while Improving Performance and Lowering Costs (SVC203...
Scale Your Application while Improving Performance and Lowering Costs (SVC203...
 
Automating Operations Workload
Automating Operations WorkloadAutomating Operations Workload
Automating Operations Workload
 
Intro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETIntro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NET
 
Automated Compliance and Governance with AWS Config and AWS CloudTrail
Automated Compliance and Governance with AWS Config and AWS CloudTrailAutomated Compliance and Governance with AWS Config and AWS CloudTrail
Automated Compliance and Governance with AWS Config and AWS CloudTrail
 
Docker on AWS
Docker on AWSDocker on AWS
Docker on AWS
 
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech TalksSave 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
Save 90% on Your Containerized Workloads - August 2017 AWS Online Tech Talks
 
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
PaaS – From Code to Running Application using AWS Elastic Beanstalk (DEV323) ...
 
VMware and AWS together (June 2017)
VMware and AWS together (June 2017)VMware and AWS together (June 2017)
VMware and AWS together (June 2017)
 
Serverless Developer Experience
Serverless Developer ExperienceServerless Developer Experience
Serverless Developer Experience
 
Deep Dive on Serverless Application Development
Deep Dive on Serverless Application DevelopmentDeep Dive on Serverless Application Development
Deep Dive on Serverless Application Development
 
Running High Availability Websites with Acquia and AWS
Running High Availability Websites with Acquia and AWSRunning High Availability Websites with Acquia and AWS
Running High Availability Websites with Acquia and AWS
 
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
(APP402) Serving Billions of Web Requests Each Day with Elastic Beanstalk | A...
 
Building faster websites: web performance with WordPress
Building faster websites: web performance with WordPressBuilding faster websites: web performance with WordPress
Building faster websites: web performance with WordPress
 

Viewers also liked

Building Real Time App
Building Real Time AppBuilding Real Time App
Building Real Time App
Futureworkz
 
Improving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornImproving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornSimon Bagreev
 
SQL vs NoSQL: 
проблема выбора
SQL vs NoSQL: 
проблема выбораSQL vs NoSQL: 
проблема выбора
SQL vs NoSQL: 
проблема выбора
TKConf
 
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Justin Gordon
 
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Justin Gordon
 
xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
Justin Gordon
 
`redux`と`flux`を比べてみたときの個人的な感想
`redux`と`flux`を比べてみたときの個人的な感想`redux`と`flux`を比べてみたときの個人的な感想
`redux`と`flux`を比べてみたときの個人的な感想
佐藤 俊太郎
 
Modern Web Development in 2015
Modern Web Development in 2015Modern Web Development in 2015
Modern Web Development in 2015
Oliver N
 
The Art of Product Marketing
The Art of Product MarketingThe Art of Product Marketing
The Art of Product Marketing
Rand Fishkin
 
Why Content Marketing Fails
Why Content Marketing FailsWhy Content Marketing Fails
Why Content Marketing FailsRand Fishkin
 

Viewers also liked (10)

Building Real Time App
Building Real Time AppBuilding Real Time App
Building Real Time App
 
Improving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and UnicornImproving Your Heroku App Performance with Asset CDN and Unicorn
Improving Your Heroku App Performance with Asset CDN and Unicorn
 
SQL vs NoSQL: 
проблема выбора
SQL vs NoSQL: 
проблема выбораSQL vs NoSQL: 
проблема выбора
SQL vs NoSQL: 
проблема выбора
 
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
Slides with notes from Ruby Conf 2014 on using simple techniques to create sl...
 
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
 
xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
 
`redux`と`flux`を比べてみたときの個人的な感想
`redux`と`flux`を比べてみたときの個人的な感想`redux`と`flux`を比べてみたときの個人的な感想
`redux`と`flux`を比べてみたときの個人的な感想
 
Modern Web Development in 2015
Modern Web Development in 2015Modern Web Development in 2015
Modern Web Development in 2015
 
The Art of Product Marketing
The Art of Product MarketingThe Art of Product Marketing
The Art of Product Marketing
 
Why Content Marketing Fails
Why Content Marketing FailsWhy Content Marketing Fails
Why Content Marketing Fails
 

Similar to React on rails v4

An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
FITC
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
jobinThomas54
 
Installing Webpack with React JS from Scratch.pdf
Installing Webpack with React JS from Scratch.pdfInstalling Webpack with React JS from Scratch.pdf
Installing Webpack with React JS from Scratch.pdf
Sufalam Technologies
 
An Intense Overview of the React Ecosystem
An Intense Overview of the React EcosystemAn Intense Overview of the React Ecosystem
An Intense Overview of the React Ecosystem
Rami Sayar
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
Jim Jones
 
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementMidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
Zach Lendon
 
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Zach Lendon
 
Rails request & middlewares
Rails request & middlewaresRails request & middlewares
Rails request & middlewares
Santosh Wadghule
 
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Udaya Kiran
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
Rami Sayar
 
React JS .NET
React JS .NETReact JS .NET
React JS .NET
Jennifer Estrada
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
Eric Berry
 
Why you should add React to your Rails application now!
Why you should add React to your Rails application now!Why you should add React to your Rails application now!
Why you should add React to your Rails application now!
David Roberts
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online Training
Learntek1
 
React-Js-Online-Training-9028522.ppsx
React-Js-Online-Training-9028522.ppsxReact-Js-Online-Training-9028522.ppsx
React-Js-Online-Training-9028522.ppsx
Kulbir4
 
Rails Request & Middlewares
Rails Request & MiddlewaresRails Request & Middlewares
Rails Request & MiddlewaresSantosh Wadghule
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHP
Taylor Lovett
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
Alessandro DS
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
Dimitri de Putte
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
MskDotNet Community
 

Similar to React on rails v4 (20)

An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
 
Installing Webpack with React JS from Scratch.pdf
Installing Webpack with React JS from Scratch.pdfInstalling Webpack with React JS from Scratch.pdf
Installing Webpack with React JS from Scratch.pdf
 
An Intense Overview of the React Ecosystem
An Intense Overview of the React EcosystemAn Intense Overview of the React Ecosystem
An Intense Overview of the React Ecosystem
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementMidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
 
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
 
Rails request & middlewares
Rails request & middlewaresRails request & middlewares
Rails request & middlewares
 
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
React JS .NET
React JS .NETReact JS .NET
React JS .NET
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Why you should add React to your Rails application now!
Why you should add React to your Rails application now!Why you should add React to your Rails application now!
Why you should add React to your Rails application now!
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online Training
 
React-Js-Online-Training-9028522.ppsx
React-Js-Online-Training-9028522.ppsxReact-Js-Online-Training-9028522.ppsx
React-Js-Online-Training-9028522.ppsx
 
Rails Request & Middlewares
Rails Request & MiddlewaresRails Request & Middlewares
Rails Request & Middlewares
 
You Got React.js in My PHP
You Got React.js in My PHPYou Got React.js in My PHP
You Got React.js in My PHP
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
 

Recently uploaded

ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 

Recently uploaded (20)

ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 

React on rails v4

  • 1. React on Rails React + Redux + React-Router on Rails Views via Webpack/NPM including Server Rendering github.com/shakacode/react_on_rails 1
  • 2. Why? • React + Redux + React-Router + Webpack + Babel Rock! • Ruby on Rails Rocks! • Let’s put them together! • Both a Ruby Gem react_on_rails and a NPM Package react-on-rails! • Used by Pivotal Tracker, Blink Inc, and others, listed here. • 1200 ⭐ as of March 18, 2016 2
  • 3. 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 large app, only some screens need a high fidelity JavaScript UX. • That’s the sweet spot: the ability to mix and match simple Rails screens with React where and when you need it, with ease! 3
  • 4. How about the react-rails gem? • react-rails is based on the standard asset pipeline inclusion of assets • Benefits • 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 rather run with the JavaScript herd for JavaScript!) 4
  • 5. Why not the react-rails gem? • The custom setup via the Rails asset pipeline is just not anything like the native tooling popular in the JavaScript community. • 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 react-bootstrap, and there was no obvious way to do that with react-rails in 2014. 5
  • 6. React on Rails: Key Parts • All files for the client side under /client • Webpack + Babel + NPM: packages files for the browser (for distribution via the Rails asset pipeline or a server for hot reloading assets) • Redux + React-Router: Key major JS components to use with React, supported by React on Rails • ESLint, JSCS, Flow, CSS-Modules, etc.: Many other cutting edge JavaScript technologies enabled by doing things the JavaScript way, and not the Rails way! 6
  • 7. React on Rails Integration with Rails • Production and Tests: Webpack creates JavaScript and CSS assets that are used by the asset pipeline, like any other JS and CSS files. • Development: Can either use statically created JS and CSS files, or special view helpers can grab “hot-reloadable” versions of the JS and CSS from a Webpack Development Server. 7
  • 8. 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! • How? • ReactOnRails view helpers, env_javascript_include_tag and env_stylesheet_link_tag configure getting assets either from HMR server or regular static files. • 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 8
  • 9. 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 defined 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. • ReactOnRails.register: JavaScript registration of stores and components (actually functions that generate them). 9
  • 10. Client Side Rendering • React on Rails installs a post-load JavaScript function (turbolinks compatible) that scans the 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. 10
  • 11. 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: 1. Sets up the JavaScript “context” with your server side webpack build 2. Create code to hydrate stores for the view 3. Create initialization code to server render react component, maybe passing props. 4. Execute this JavaScript on the Rails Server as part of the rendering process 5. Returns a HTML string placed on the Rails view 11
  • 12. Shared Redux Stores AppReduxStore Header React Component Body React Component Footer React Component • Many React components communicating with the same redux store • Supports server rendering and react-router 12
  • 13. Shared Redux Stores AppReduxStore 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! Notifications indicators! • Why use Rails views? Sometimes Rails is good enough! and WAY less code! Header React Component AppReduxStore 13
  • 14. react-router Integration • 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. 14
  • 15. CSS Options 1. Standard Rails: Just use React on Rails for JavaScript and your regular Rails techniques for Sass, images, etc. Advantage: simple 2. Webpack Generated CSS: More complex, but not hard to setup with our examples. Advantages: 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 files right next to your JavaScript. Here’s an example in the react-webpack-rails-tutorial. 2. Hot Reloading. You can change the your Sass files, save, and then you’ll see the browser update, without reloading. For configuration of hot reloading, see Hot Reloading of Assets For Rails Development. 15
  • 16. Performance • Turbolinks: Don’t reload the JavaScript and CSS, just the HTML • Fragment caching of Server Rendering: The fast way to generate the HTML is to have it cached! 16
  • 17. Running Tests • 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 config/initializers/react_on_rails.config • Configure 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
 ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) 17
  • 18. Getting Started • Read The React on Rails Doctrine • 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/master/spec/dummy • A more complex example at www.reactrails.com with code: https:// github.com/shakacode/react-webpack-rails-tutorial 18
  • 19. Want Personalized Help? • ShakaCode offers consulting on React on Rails, from a modestly priced starter pack of usage help ($1500) to full app development. We’re experts at Rails/React/Redux/React-Router/Webpack JavaScript apps! • Check out forum.shakacode.com for code discussions. • Ask Justin for a Slack invite. • Get in touch with Justin: justin@shakacode.com. • Follow @railsonmaui for updates on React on Rails. • Aloha from Justin and the ShakaCode Team! 19