SlideShare une entreprise Scribd logo
1  sur  52
React & Redux in Hulu
程墨 (Morgan Cheng)
@morgancheng
About Me
About Hulu
 System Complexity
 Legacy code in jQuery & Backbone
 UI non-predictable
 Lack of component isolation
 Performance Bottleneck
 Initial AJAX roundtrip for browser-side-rendering
Challenges
jQuery
Backbone
Ruby on Rails
React
Redux
Node + Express
Tech Stack
React vs. jQuery
Code in jQuery
Virtual DOM DOM
<div>
<Foo/>
<Bar>children</Bar>
</div>
JSX
Virtual DOM DOM
<div>
<Foo/>
<Bar>children</Bar>
</div>
JSX
Previous Virtual DOM
Virtual DOM DOM
<div>
<Foo/>
<Bar>children</Bar>
</div>
JSX
Previous Virtual DOM
Diff
Virtual DOM DOM
<div>
<Foo/>
<Bar>children</Bar>
</div>
JSX
Previous Virtual DOM
Diff
React App = Keep Rendering
UI = f(state)
React mixes JS/Style/Markup
if (process.env.BROWSER) {
require('./style.scss');
}
const divStyle = {
color: ‘blue’
};
function render() {
return (
<div onClick={this.onClick} style={divStyle}>Click Me</div>
);
}
Isomorphic/Universal React
const appHtml = ReactDOMServer.renderToString(component);
Server Side
Browser Side
ReactDOM.render(component,
window.document.getElementById("appRoot"));
React Components are bricks, not building
Problem of MVC
Flux
 Inter-dependency among multiple stores
 Hard to support server-side-rendering
 Hard to do hot-reloading
Drawbacks of Flux
Redux = Reducer + Flux
Reducer
(state, action) => newState
Redux
Store
Action Reducer
State
ReducerReducer
View
dispatch
From Perspective of Component
React View
Action Creators
Reducer
State Key
export {view, actions, reducer, stateKey}
Hulu Component
{
foo: { ... }
bar: { ... }
}
State
Reducer
{
foo: { ... }
bar: { ... }
}
Action
React View
React + Redux = Win!
We get Performance
and Maintainability
at the same time with
React & Redux
Without Redux With Redux
Store
Constraints over Conventions
 jQuery Dies Hard
 Legacy Code
 Third Party JavaScript Code
 e.g. Optimizely
React conflicts with jQuery
Virtual DOM DOM
React
jQuery
React conflicts with jQuery
Virtual DOM DOM
React
jQuery
render() componentDidUpdate()componentWillUpdate ()
componentWillMount() render() componentDidMount()
React Mounting Phase
React Updating Phase
invoked only at browser side
not invoked for first time render
class Foo extends React.Component{
componentDidMount() {
// do DOM manipulation with jQuery
}
componentDidUpdate() {
// do DOM manipulation with jQuery
}
render() {
// render JSX for react
}
}
react render
jQuery render
react render
jQuery render
Dehydrate & Rehydrate
State React HTML
Server Side
Browser Side
+ =
Dehydrated State React
+ = Rehydrated HTML
Page A
Web Server API Server
Page Request
HTML
&
Dehydrated Data
API Request
Page A Page B
Web Server API Server
API Request Raw Response Data
Chunked Code
On SPA Navigation
Page A Page B
Reset Singleton Store with
new Reducer and State
Faster React Server Rendering
 Set Production Mode
 NODE_ENV=production node app.js
 Use Minified React.js
 require(‘react/dist/react.min.js’);
 After v0.14, DO_NOT_USE_OR_YOU_WILL_BE_FIRED
 Babel-react-optimize Preset
 https://github.com/thejameskyle/babel-react-optimize
Avoid Wasted-Rendering-Time
Virtual DOM
Previous Virtual DOM
Waste If Same
Avoid Dynamic props
<Foo onClick={ function() { doSomething(); } } />
<Bar style={ {margin: "0px" } }/>
Incremental Page Rendering
React-Router-Redux
<Provider store={store} >
<Router history={history}>
<Route path="/" component={App}>
<Route path="foo" component={Foo} />
<Route path="bar" component={Bar} />
<Route>
</Router>
<Provider>
Maps Path to Page Component
Server Rendering Process
Fetching State
Rendering Components
Render Dehydrated State
fetchState().then(render)
ReactDOMServer.renderToString(...)
<script>
window.initState = ...
</script>
Page Should Not Be Component
class Page {
getComponents() {
return [
Header,
Foo,
Bar,
Footer
];
}
}
Server Rendering Process Improved
Start Fetching State
Rendering Component
Render Dehydrated State
Render Dehydrated State
Rendering Component
Rendering Component
Render Dehydrated State
Push After Every Render
Constraint over Conventions
Search Hulu in WeChat
Thanks
Any Question?

Contenu connexe

Tendances

Tendances (20)

React
React React
React
 
Grunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous IntegrationGrunt.js and Yeoman, Continous Integration
Grunt.js and Yeoman, Continous Integration
 
Web components - An Introduction
Web components - An IntroductionWeb components - An Introduction
Web components - An Introduction
 
GWT: Our Experiences
GWT: Our ExperiencesGWT: Our Experiences
GWT: Our Experiences
 
Passo a Passo para criar uma aplicação Móvel Híbrida
Passo a Passo para criar uma aplicação Móvel HíbridaPasso a Passo para criar uma aplicação Móvel Híbrida
Passo a Passo para criar uma aplicação Móvel Híbrida
 
GWT- Google Web Toolkit
GWT- Google Web ToolkitGWT- Google Web Toolkit
GWT- Google Web Toolkit
 
Korea linuxforum2014 html5game-sangseoklim
Korea linuxforum2014 html5game-sangseoklimKorea linuxforum2014 html5game-sangseoklim
Korea linuxforum2014 html5game-sangseoklim
 
What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0What's new in Java EE 7? From HTML5 to JMS 2.0
What's new in Java EE 7? From HTML5 to JMS 2.0
 
Laravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleLaravel 8 export data as excel file with example
Laravel 8 export data as excel file with example
 
Vaadin Introduction, 7.3 edition
Vaadin Introduction, 7.3 editionVaadin Introduction, 7.3 edition
Vaadin Introduction, 7.3 edition
 
Introduction to Vaadin
Introduction to VaadinIntroduction to Vaadin
Introduction to Vaadin
 
Vaadin Components @ Angular U
Vaadin Components @ Angular UVaadin Components @ Angular U
Vaadin Components @ Angular U
 
Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014Modern Web Application Development Workflow - EclipseCon Europe 2014
Modern Web Application Development Workflow - EclipseCon Europe 2014
 
An Introduction to Vuejs
An Introduction to VuejsAn Introduction to Vuejs
An Introduction to Vuejs
 
Vaadin Components
Vaadin ComponentsVaadin Components
Vaadin Components
 
Booting up with polymer
Booting up with polymerBooting up with polymer
Booting up with polymer
 
Vaadin & Web Components
Vaadin & Web ComponentsVaadin & Web Components
Vaadin & Web Components
 
Love at first Vue
Love at first VueLove at first Vue
Love at first Vue
 
Managing JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSManaging JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJS
 
Hardware Acceleration in WebKit
Hardware Acceleration in WebKitHardware Acceleration in WebKit
Hardware Acceleration in WebKit
 

Similaire à React & Redux in Hulu

Секретный доклад о React Router - OdessaJS 2014
Секретный доклад о React Router - OdessaJS 2014Секретный доклад о React Router - OdessaJS 2014
Секретный доклад о React Router - OdessaJS 2014
Andrey Listochkin
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScript
jasonsich
 

Similaire à React & Redux in Hulu (20)

Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJS
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
The Exciting Future Of React
The Exciting Future Of ReactThe Exciting Future Of React
The Exciting Future Of React
 
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
 
Секретный доклад о React Router - OdessaJS 2014
Секретный доклад о React Router - OdessaJS 2014Секретный доклад о React Router - OdessaJS 2014
Секретный доклад о React Router - OdessaJS 2014
 
React Vs AnagularJS
React Vs AnagularJSReact Vs AnagularJS
React Vs AnagularJS
 
Introduction to React and MobX
Introduction to React and MobXIntroduction to React and MobX
Introduction to React and MobX
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScript
 
ReactJS
ReactJSReactJS
ReactJS
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
OttawaJS - React
OttawaJS - ReactOttawaJS - React
OttawaJS - React
 
React js
React jsReact js
React js
 
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
Universal JS Web Applications with React - Web Summer Camp 2017, Rovinj (Work...
 
React native by example by Vadim Ruban
React native by example by Vadim RubanReact native by example by Vadim Ruban
React native by example by Vadim Ruban
 
Tech Talk on ReactJS
Tech Talk on ReactJSTech Talk on ReactJS
Tech Talk on ReactJS
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to React
 
HTML alchemy: the secrets of mixing JavaScript and Java EE - Matthias Wessendorf
HTML alchemy: the secrets of mixing JavaScript and Java EE - Matthias WessendorfHTML alchemy: the secrets of mixing JavaScript and Java EE - Matthias Wessendorf
HTML alchemy: the secrets of mixing JavaScript and Java EE - Matthias Wessendorf
 
Academy PRO: React JS
Academy PRO: React JSAcademy PRO: React JS
Academy PRO: React JS
 

Plus de Morgan Cheng (13)

Flux and redux
Flux and reduxFlux and redux
Flux and redux
 
Critical thinking in Node.js
Critical thinking in Node.jsCritical thinking in Node.js
Critical thinking in Node.js
 
Engineering excellence 卓越工程
Engineering excellence 卓越工程Engineering excellence 卓越工程
Engineering excellence 卓越工程
 
High Performance Mobile Web
High Performance Mobile WebHigh Performance Mobile Web
High Performance Mobile Web
 
YUI vs jQuery: to Build Large Scale JavaScript App
YUI vs jQuery: to Build Large Scale JavaScript AppYUI vs jQuery: to Build Large Scale JavaScript App
YUI vs jQuery: to Build Large Scale JavaScript App
 
Single Page WebApp Architecture
Single Page WebApp ArchitectureSingle Page WebApp Architecture
Single Page WebApp Architecture
 
Secrets of Effective Presentation
Secrets of Effective PresentationSecrets of Effective Presentation
Secrets of Effective Presentation
 
Build Lightweight Web Module
Build Lightweight Web ModuleBuild Lightweight Web Module
Build Lightweight Web Module
 
Optimize URL for Performance
Optimize URL for PerformanceOptimize URL for Performance
Optimize URL for Performance
 
F2E's Creeds
F2E's CreedsF2E's Creeds
F2E's Creeds
 
Comet Server Push Over Web
Comet Server Push Over WebComet Server Push Over Web
Comet Server Push Over Web
 
Mobile Web on Touch Event and YUI
Mobile Web on Touch Event and YUIMobile Web on Touch Event and YUI
Mobile Web on Touch Event and YUI
 
Embracing YUI3 and Frontend Perf
Embracing YUI3 and Frontend PerfEmbracing YUI3 and Frontend Perf
Embracing YUI3 and Frontend Perf
 

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@
 

Dernier (20)

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
 
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...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
+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...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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?
 
[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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

React & Redux in Hulu

Notes de l'éditeur

  1. &amp;lt;number&amp;gt;
  2. All my experience is about trade off between complexity and performance. &amp;lt;number&amp;gt;
  3. Hulu is Subscription-based video provider. &amp;lt;number&amp;gt;
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;
  6. The migration of tech stack. Not just because React &amp; Redux is cool and new, but it solves OUR specific problems. &amp;lt;number&amp;gt;
  7. jQuery is created for a reason, but React solves the same problem in more understandable way. &amp;lt;number&amp;gt;
  8. jQuery code is so easy to be written into a mess. &amp;lt;number&amp;gt;
  9. VirtualDOM magic of React/JSX &amp;lt;number&amp;gt;
  10. The ensense of React and Redux &amp;lt;number&amp;gt;
  11. React is actually implementing web-component. HTML, CSS and JS is consolidated into one component. &amp;lt;number&amp;gt;
  12. Just write one component which can be rendered in browser OR in server. It doesn’t matter. Isomorphic(同构) React &amp;lt;number&amp;gt;
  13. React is just another jQuery. d It helps to build component, but not work alone for big app. &amp;lt;number&amp;gt;
  14. Strictly, Model should not communicate directly with View. But, it is easy to be violated. &amp;lt;number&amp;gt;
  15. Flux is uni-direction flow. It doesn’t make sense to match parts between MVC and Flux &amp;lt;number&amp;gt;
  16. &amp;lt;number&amp;gt;
  17. Redux is a better flux/fluxable. &amp;lt;number&amp;gt;
  18. Redux is a better flux/fluxable. &amp;lt;number&amp;gt;
  19. View: What content should I rendered? Actions: What ”events” can I respond? Reducer: How to update state according to actions? State Key: The sub state key controlled by this component &amp;lt;number&amp;gt;
  20. Data Flow &amp;lt;number&amp;gt;
  21. React &amp; Redux enforce functional programming model; isomorphic javascript. Better data management. &amp;lt;number&amp;gt;
  22. React &amp; Redux enforce functional programming model; isomorphic JavaScript. All state resides in singleton store. &amp;lt;number&amp;gt;
  23. From perspective of architecture, we should enforce constraints instead of just asking people to follow conventions. React &amp; Redux enforce functional programming model; isomorphic JavaScript. Better data management. &amp;lt;number&amp;gt;
  24. Optimizely operates perfectly with jQuery, but is not designed for React. &amp;lt;number&amp;gt;
  25. Rehydrated HTML must exactly match HTML from server. Otherwise, user will see UI flash. React do this by checking checksum attribute in generated HTML. &amp;lt;number&amp;gt;
  26. Work flow of first page access. &amp;lt;number&amp;gt;
  27. One piece JavaScript for the whole application might be too big. Need to be chunked. &amp;lt;number&amp;gt;
  28. In Single Page Application with chunked code, each page need to reset singleton store to enable new reducer and new stae. &amp;lt;number&amp;gt;
  29. React is not designed for Server according to FB. But, there are some tricks to make it faster. &amp;lt;number&amp;gt;
  30. &amp;lt;number&amp;gt;
  31. Even React helps us to avoid actual DOM manipulation time, it is still a waste if Virtual DOM is actually same in re-rendering. &amp;lt;number&amp;gt;
  32. &amp;lt;number&amp;gt;
  33. React-Router-Redux maps URL path to Page Component. Each Page Component is actually a React Component. Has to be rendered in sync way. &amp;lt;number&amp;gt;
  34. &amp;lt;number&amp;gt;
  35. Page should just declare which components it contains, and let framework do the incremental rendering. &amp;lt;number&amp;gt;
  36. &amp;lt;number&amp;gt;