SlideShare une entreprise Scribd logo
1  sur  37
How we use React.js
A case study in completely re-implementing your UI in one month
Iron Yard Crystal City - Tuesday April 7, 2015
Hi, I’m Geoff
• CTO and Cofounder at Cortex
• Began career as an analyst and project
manager
• Began development as a hobby
(interested in baseball stats)
• Fell in love with Ruby after initial
flirtation with PHP
• Started Five Tool Development, a web
development agency
What does Cortex do?
• Our application provides commercial building operators and
engineers with minute-to-minute recommendations about energy use
and live data from hundreds of sensors and meters
• Our first building saved half a million dollars in energy costs during
their first year using Cortex
• 95% of our customer use is through mobile apps. We deliver a
responsive web application in a WebView container to iOS and
Android
What we’re going to talk about
• Super-fast overview of what React is
• How we used React to dramatically improve Cortex
• Even faster overview of Flux
• Questions (feel free to stop me any time)
Problem
Building engineers frequently work in spaces with no network
connectivity.
They expect to be able to get their data everywhere.
Complication
Need to support three platforms (web, iOS, Android) with limited
resources.
We have already invested hundreds of hours in a first-class HTML-
based interface.
The Plan
Turn our webapp into a persistently-open single-page application
(SPA). Users will be on the same page all the time, so the application
will never reload in the browser.
Our application will fetch and cache data when the network is available,
and present it even when the network is unavailable.
What is MVC?
Source: Google (https://developer.chrome.com/apps/app_frameworks)
Lots of JavaScript MVC Frameworks
Compared to Ruby, JavaScript is the
Wild (Wild) West.
Space is less mature, but filled with really
interesting new innovations and ideas.
Cutting-edge solutions, but not a lot of
consensus around which are the best yet.
Source: James T. West
Backbone
• Created by Jeremy Ashkenas from the New
York Times visualizations group
(he built Coffeescript, Underscore.js)
• Early pioneer in JavaScript MVC frameworks
• Focus on data sync between browser and
server-based API, leaves view/presentation
layer up to developer
Ember
• Created by Yehuda Katz (of Rails- and jQuery-core fame) and Tom Dale
• Uses two-way data binding
• Very opinionated, does things “the Ember Way”
• Explicitly for single-page apps
• Has awesome routing
• Liked by many Rails developers
Angular
• Developed at Google
• Uses two-way data binding
• Less opinionated than Ember, which means it
is more flexible, but also requires you to do
more heavy lifting yourself
• Not just for SPAs
• Extends HTML with custom attributes
React
• Developed at Facebook
• Unidirectional flow of data (one-way binding)
• Re-renders everything when something
changes, uses a “virtual DOM” to calculate
differences
• Can be implemented in small pieces, does not
require full-scale use across application
• Younger than previous options, rapidly growing
With or without you
(by “you”, I mean a network)
• Cache data in browser using
localStorage API
• Always be on the same page,
never reload the page due to user
clicks
• Give friendly messages when
network is unavailable
Source: The Edge
The need for speed
• Context changes should be snappy
• Pre-fetch data the user will likely
want and put it into localStorage
before the user requests it
• Minimize delays in rendering UI
components due to cascading
changes with asynchronous
execution Source: Miramar Naval Air Station, aka “Top Gun”
So how does React work?
• React’s building blocks are Components
• Components are JavaScript objects tasked with outputting HTML
• Only requirement is a render() function that returns HTML
• Components can maintain their own internal state
A dumb component
<div class=“commentBox”>
Hello, World! I am a CommentBox.
</div>
var CommentBox = React.createClass({
render: function() {
return (
<div className="commentBox">
Hello, world! I am a CommentBox.
</div>
);
}
});
React.createElement(CommentBox,{});
A slightly less dumb component
<div class=“greeting”>
Welcome, Homer
</div>
var Greeting = React.createClass({
render: function() {
return (
<div className="greeting">
Welcome, { this.props.username }
</div>
);
}
});
React.createElement(
Greeting,
{ username: “Homer” }
);
A slightly smart component
var UserList = React.createComponent({
getInitialState: function() {
return { users: [] };
},
users: function() {
return _.map(
this.state.users,
function(user) {
return (
<li className="user">{ user.name }</li>
);
},
)
},
render: function() {
return (
<ul className="users">
{ this.users() }
</ul>
);
}
# with no users (at initialization)
<ul class=“users”></ul>
# with one user (state changed)
<ul class=“users”>
<li class=“user”>Geoff</li>
</ul>
# with two more users and one removed
<ul class=“users”>
<li class=“user”>James</li>
<li class=“user”>Su</li>
</ul>
Smarter is not better
• Props are immutable, easier to track
• Favor props over state whenever possible
• Make component functions as dead-simple as you can
• Dumber components are easier to test, and likely faster to render
Components nest
• Any complicated element or repeated set of elements should be its
own component
• When the parent re-renders, React figures out whether or not the
children should remain, be updated, or be removed
• Components can be passed in as props to a component or returned
from a function in a component
• React can render an array of components
React lifecycle
• React offers a number of “hooks” that allow you to trigger events at
certain points in the Component’s lifecycle
• Examples of lifecycle events: when a component renders, when it gets
updated state, when it is removed from the DOM
• Uses: start a timer, listen for UI events, tie in jQuery or other libraries
after a component is rendered, then remove those timers/listeners when
the component is unmounted
• Cortex uses lifecycle to trigger chart drawing with d3 and updates every
30 seconds
Use functions liberally
• Functions are fast (mostly)
• Keep logic in functions outside of render().
render() should look as much like HTML as
possible.
• Just like your Ruby code, many short
methods with limited arguments makes for
cleaner, easier to test code than one
massive God method that does all the
decision-making Source: Arkansas
Shared behavior
• React components share behavior through Mixins
• Mixins don’t provide the same inheritance or composition capabilities
as Ruby (no overriding methods or implementations)
• Example of shared behavior: all cards that collapse or expand have
the same collapse/expand mixin
Lots of components!
Page ->
Dashboard ->
Navigation ->
Navigation Item(s)
Card Collection ->
FlashMessage(s)
Card(s) ->
Card Header
Card Chart
Card Tab
Lots of components!
Let’s take a look at the components in Cortex
React tips
• Don’t manipulate HTML yourself!
React can’t keep track of HTML changes you make after rendering. If
you’re tempted to use jQuery or another library to alter HTML, figure
out a way to do it through the component’s props and state.
• Don’t be afraid to break components into smaller sub-components
More React tips
• Components can communicate with their parents through functions.
Pass a function from the parent to the child as a prop, then the child
can call it and send a message to the parent. Super useful, but don’t
overdo it!
• Start small. One of the best things about React is that you can
implement it in little pieces in your UI as you find opportunities to use
it.
What about global state?
• In our first attempt at using React, we had a top-level “Page”
component that rendered all the various views inside of itself. The
Page was responsible for maintaining global state such as the date
being viewed, the current building, and the desired page.
• Components would pass the global state as properties to children
who often did nothing but just pass that state on again to their own
children
• It felt messy. It was!
Enter Flux
• Flux is Facebook’s answer to storing global state in React apps
• Flux data is kept in “stores”, which announce changes to React
components, which can update stores with “actions”
• A dispatcher ensures that updates that depend on other updates
don’t get deadlocked
Flux Architecture
Source: Facebook
What did we lose?
Here’s how the application has gotten worse:
• Application is considerably more complicated than when it was just
Rails and jQuery (this would have happened with any of the other JS
frameworks)
• More places things can break in our code
• React is much harder to test well than Rails with Capybara
• Some co-mingling of business logic and our presentation layer
What did we accomplish?
Here’s how the application has improved:
• Application is much faster from the user’s perspective
• Feels more “native”, with no reloads and quick changes after clicks
• App is useful when the network isn't available
• Ruby is responsible for our server-side work, and JavaScript owns
the browser and presentation layer
Further reading
• React.js: https://facebook.github.io/react/
• Flux: https://facebook.github.io/flux/
• Marty.js (a great Flux implementation): http://martyjs.org/
• React-rails gem
(mount React components in views, compile JSX in asset pipeline):
https://github.com/reactjs/react-rails
Questions?
I’m happy to answer questions about:
• React and Flux
• Ruby or Rails
• Cortex
• Working as a developer
• The awful state of the Boston Red Sox’ starting rotation
Thanks!
Feel free to get in touch!
twitter: @geoffharcourt
github: geoffharcourt
email: geoff@fivetool.io

Contenu connexe

Tendances

Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and
Ryan Cuprak
 
Should you use HTML5 to build your product? The pros & cons of using current ...
Should you use HTML5 to build your product? The pros & cons of using current ...Should you use HTML5 to build your product? The pros & cons of using current ...
Should you use HTML5 to build your product? The pros & cons of using current ...
boxuno
 

Tendances (20)

Flexible Permissions Management with ACL Templates
Flexible Permissions Management with ACL TemplatesFlexible Permissions Management with ACL Templates
Flexible Permissions Management with ACL Templates
 
DEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
DEV117 - Unleash the Power of the AppDev Pack and Node.js in DominoDEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
DEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
 
Alfresco DevCon 2019 Performance Tools of the Trade
Alfresco DevCon 2019   Performance Tools of the TradeAlfresco DevCon 2019   Performance Tools of the Trade
Alfresco DevCon 2019 Performance Tools of the Trade
 
Using Cool New Frameworks in (Mobile) Domino Apps
Using Cool New Frameworks in (Mobile) Domino AppsUsing Cool New Frameworks in (Mobile) Domino Apps
Using Cool New Frameworks in (Mobile) Domino Apps
 
Flexible UI Components for a Multi-Framework World
Flexible UI Components for a Multi-Framework WorldFlexible UI Components for a Multi-Framework World
Flexible UI Components for a Multi-Framework World
 
Alfresco Digital Business Platform Builder Experience
Alfresco Digital Business Platform Builder ExperienceAlfresco Digital Business Platform Builder Experience
Alfresco Digital Business Platform Builder Experience
 
Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and Hybrid Mobile Development with Apache Cordova and
Hybrid Mobile Development with Apache Cordova and
 
Building Reactive webapp with React/Flux
Building Reactive webapp with React/FluxBuilding Reactive webapp with React/Flux
Building Reactive webapp with React/Flux
 
Thinking in Components
Thinking in ComponentsThinking in Components
Thinking in Components
 
AD1545 - Extending the XPages Extension Library
AD1545 - Extending the XPages Extension LibraryAD1545 - Extending the XPages Extension Library
AD1545 - Extending the XPages Extension Library
 
FITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive ImagesFITC - Exploring Art-Directed Responsive Images
FITC - Exploring Art-Directed Responsive Images
 
Dev112 let's calendar that
Dev112   let's calendar thatDev112   let's calendar that
Dev112 let's calendar that
 
Optimus XPages: An Explosion of Techniques and Best Practices
Optimus XPages: An Explosion of Techniques and Best PracticesOptimus XPages: An Explosion of Techniques and Best Practices
Optimus XPages: An Explosion of Techniques and Best Practices
 
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
 
React & Flux Workshop
React & Flux WorkshopReact & Flux Workshop
React & Flux Workshop
 
Presenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View ControlPresenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View Control
 
. Design Decisions: Developing for Mobile - The Template Experience Project
. Design Decisions: Developing for Mobile - The Template Experience Project. Design Decisions: Developing for Mobile - The Template Experience Project
. Design Decisions: Developing for Mobile - The Template Experience Project
 
A Day of REST
A Day of RESTA Day of REST
A Day of REST
 
Move Your XPages Applications to the Fast Lane
Move Your XPages Applications to the Fast LaneMove Your XPages Applications to the Fast Lane
Move Your XPages Applications to the Fast Lane
 
Should you use HTML5 to build your product? The pros & cons of using current ...
Should you use HTML5 to build your product? The pros & cons of using current ...Should you use HTML5 to build your product? The pros & cons of using current ...
Should you use HTML5 to build your product? The pros & cons of using current ...
 

Similaire à React.js at Cortex

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
 
React.js - and how it changed our thinking about UI
React.js - and how it changed our thinking about UIReact.js - and how it changed our thinking about UI
React.js - and how it changed our thinking about UI
Marcin Grzywaczewski
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
SHAIKIRFAN715544
 

Similaire à React.js at Cortex (20)

React introduction
React introductionReact introduction
React introduction
 
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
 
React.js - and how it changed our thinking about UI
React.js - and how it changed our thinking about UIReact.js - and how it changed our thinking about UI
React.js - and how it changed our thinking about UI
 
Meteor + React
Meteor + ReactMeteor + React
Meteor + React
 
Introduction to react native with redux
Introduction to react native with reduxIntroduction to react native with redux
Introduction to react native with redux
 
Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016
 
React Tech Salon
React Tech SalonReact Tech Salon
React Tech Salon
 
Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"
 
ReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparisonReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparison
 
Learning React - I
Learning React - ILearning React - I
Learning React - I
 
O365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialO365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - Material
 
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...Дмитрий Тежельников  «Разработка вэб-решений с использованием Asp.NET.Core и ...
Дмитрий Тежельников «Разработка вэб-решений с использованием Asp.NET.Core и ...
 
An Overview of the React Ecosystem
An Overview of the React EcosystemAn Overview of the React Ecosystem
An Overview of the React Ecosystem
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
Single Page Applications: Your Browser is the OS!
Single Page Applications: Your Browser is the OS!Single Page Applications: Your Browser is the OS!
Single Page Applications: Your Browser is the OS!
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
 
Fluxible
FluxibleFluxible
Fluxible
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

React.js at Cortex

  • 1. How we use React.js A case study in completely re-implementing your UI in one month Iron Yard Crystal City - Tuesday April 7, 2015
  • 2. Hi, I’m Geoff • CTO and Cofounder at Cortex • Began career as an analyst and project manager • Began development as a hobby (interested in baseball stats) • Fell in love with Ruby after initial flirtation with PHP • Started Five Tool Development, a web development agency
  • 3. What does Cortex do? • Our application provides commercial building operators and engineers with minute-to-minute recommendations about energy use and live data from hundreds of sensors and meters • Our first building saved half a million dollars in energy costs during their first year using Cortex • 95% of our customer use is through mobile apps. We deliver a responsive web application in a WebView container to iOS and Android
  • 4.
  • 5. What we’re going to talk about • Super-fast overview of what React is • How we used React to dramatically improve Cortex • Even faster overview of Flux • Questions (feel free to stop me any time)
  • 6. Problem Building engineers frequently work in spaces with no network connectivity. They expect to be able to get their data everywhere.
  • 7. Complication Need to support three platforms (web, iOS, Android) with limited resources. We have already invested hundreds of hours in a first-class HTML- based interface.
  • 8. The Plan Turn our webapp into a persistently-open single-page application (SPA). Users will be on the same page all the time, so the application will never reload in the browser. Our application will fetch and cache data when the network is available, and present it even when the network is unavailable.
  • 9. What is MVC? Source: Google (https://developer.chrome.com/apps/app_frameworks)
  • 10. Lots of JavaScript MVC Frameworks Compared to Ruby, JavaScript is the Wild (Wild) West. Space is less mature, but filled with really interesting new innovations and ideas. Cutting-edge solutions, but not a lot of consensus around which are the best yet. Source: James T. West
  • 11. Backbone • Created by Jeremy Ashkenas from the New York Times visualizations group (he built Coffeescript, Underscore.js) • Early pioneer in JavaScript MVC frameworks • Focus on data sync between browser and server-based API, leaves view/presentation layer up to developer
  • 12. Ember • Created by Yehuda Katz (of Rails- and jQuery-core fame) and Tom Dale • Uses two-way data binding • Very opinionated, does things “the Ember Way” • Explicitly for single-page apps • Has awesome routing • Liked by many Rails developers
  • 13. Angular • Developed at Google • Uses two-way data binding • Less opinionated than Ember, which means it is more flexible, but also requires you to do more heavy lifting yourself • Not just for SPAs • Extends HTML with custom attributes
  • 14. React • Developed at Facebook • Unidirectional flow of data (one-way binding) • Re-renders everything when something changes, uses a “virtual DOM” to calculate differences • Can be implemented in small pieces, does not require full-scale use across application • Younger than previous options, rapidly growing
  • 15. With or without you (by “you”, I mean a network) • Cache data in browser using localStorage API • Always be on the same page, never reload the page due to user clicks • Give friendly messages when network is unavailable Source: The Edge
  • 16. The need for speed • Context changes should be snappy • Pre-fetch data the user will likely want and put it into localStorage before the user requests it • Minimize delays in rendering UI components due to cascading changes with asynchronous execution Source: Miramar Naval Air Station, aka “Top Gun”
  • 17. So how does React work? • React’s building blocks are Components • Components are JavaScript objects tasked with outputting HTML • Only requirement is a render() function that returns HTML • Components can maintain their own internal state
  • 18. A dumb component <div class=“commentBox”> Hello, World! I am a CommentBox. </div> var CommentBox = React.createClass({ render: function() { return ( <div className="commentBox"> Hello, world! I am a CommentBox. </div> ); } }); React.createElement(CommentBox,{});
  • 19. A slightly less dumb component <div class=“greeting”> Welcome, Homer </div> var Greeting = React.createClass({ render: function() { return ( <div className="greeting"> Welcome, { this.props.username } </div> ); } }); React.createElement( Greeting, { username: “Homer” } );
  • 20. A slightly smart component var UserList = React.createComponent({ getInitialState: function() { return { users: [] }; }, users: function() { return _.map( this.state.users, function(user) { return ( <li className="user">{ user.name }</li> ); }, ) }, render: function() { return ( <ul className="users"> { this.users() } </ul> ); } # with no users (at initialization) <ul class=“users”></ul> # with one user (state changed) <ul class=“users”> <li class=“user”>Geoff</li> </ul> # with two more users and one removed <ul class=“users”> <li class=“user”>James</li> <li class=“user”>Su</li> </ul>
  • 21. Smarter is not better • Props are immutable, easier to track • Favor props over state whenever possible • Make component functions as dead-simple as you can • Dumber components are easier to test, and likely faster to render
  • 22. Components nest • Any complicated element or repeated set of elements should be its own component • When the parent re-renders, React figures out whether or not the children should remain, be updated, or be removed • Components can be passed in as props to a component or returned from a function in a component • React can render an array of components
  • 23. React lifecycle • React offers a number of “hooks” that allow you to trigger events at certain points in the Component’s lifecycle • Examples of lifecycle events: when a component renders, when it gets updated state, when it is removed from the DOM • Uses: start a timer, listen for UI events, tie in jQuery or other libraries after a component is rendered, then remove those timers/listeners when the component is unmounted • Cortex uses lifecycle to trigger chart drawing with d3 and updates every 30 seconds
  • 24. Use functions liberally • Functions are fast (mostly) • Keep logic in functions outside of render(). render() should look as much like HTML as possible. • Just like your Ruby code, many short methods with limited arguments makes for cleaner, easier to test code than one massive God method that does all the decision-making Source: Arkansas
  • 25. Shared behavior • React components share behavior through Mixins • Mixins don’t provide the same inheritance or composition capabilities as Ruby (no overriding methods or implementations) • Example of shared behavior: all cards that collapse or expand have the same collapse/expand mixin
  • 26. Lots of components! Page -> Dashboard -> Navigation -> Navigation Item(s) Card Collection -> FlashMessage(s) Card(s) -> Card Header Card Chart Card Tab
  • 27. Lots of components! Let’s take a look at the components in Cortex
  • 28. React tips • Don’t manipulate HTML yourself! React can’t keep track of HTML changes you make after rendering. If you’re tempted to use jQuery or another library to alter HTML, figure out a way to do it through the component’s props and state. • Don’t be afraid to break components into smaller sub-components
  • 29. More React tips • Components can communicate with their parents through functions. Pass a function from the parent to the child as a prop, then the child can call it and send a message to the parent. Super useful, but don’t overdo it! • Start small. One of the best things about React is that you can implement it in little pieces in your UI as you find opportunities to use it.
  • 30. What about global state? • In our first attempt at using React, we had a top-level “Page” component that rendered all the various views inside of itself. The Page was responsible for maintaining global state such as the date being viewed, the current building, and the desired page. • Components would pass the global state as properties to children who often did nothing but just pass that state on again to their own children • It felt messy. It was!
  • 31. Enter Flux • Flux is Facebook’s answer to storing global state in React apps • Flux data is kept in “stores”, which announce changes to React components, which can update stores with “actions” • A dispatcher ensures that updates that depend on other updates don’t get deadlocked
  • 33. What did we lose? Here’s how the application has gotten worse: • Application is considerably more complicated than when it was just Rails and jQuery (this would have happened with any of the other JS frameworks) • More places things can break in our code • React is much harder to test well than Rails with Capybara • Some co-mingling of business logic and our presentation layer
  • 34. What did we accomplish? Here’s how the application has improved: • Application is much faster from the user’s perspective • Feels more “native”, with no reloads and quick changes after clicks • App is useful when the network isn't available • Ruby is responsible for our server-side work, and JavaScript owns the browser and presentation layer
  • 35. Further reading • React.js: https://facebook.github.io/react/ • Flux: https://facebook.github.io/flux/ • Marty.js (a great Flux implementation): http://martyjs.org/ • React-rails gem (mount React components in views, compile JSX in asset pipeline): https://github.com/reactjs/react-rails
  • 36. Questions? I’m happy to answer questions about: • React and Flux • Ruby or Rails • Cortex • Working as a developer • The awful state of the Boston Red Sox’ starting rotation
  • 37. Thanks! Feel free to get in touch! twitter: @geoffharcourt github: geoffharcourt email: geoff@fivetool.io