Best Practice
React.js
SEA.YANG @ Jun 2016
data => view
Keynote Speakers
Tom Occhino Nick SchrockPete Hunt
React is much more than a JS library
React is an ecosystem
A fundamental advance for
web and mobile development
— Nick Schrock, React.js Conf 2016
Best Practice
React
ES6
Redux (Flux)
Immutable-JS
React-redux
GraphQL
Relay
Normalizr
Babel
React-router
……
Facebook is investing in javascript
React.js
Jun 2013 Mar 2015
Facebook
Inner Project
Public
Release v15.1.0
May 2016
May 2015
Initial Version
for iOS v0.26.2
May 2016Sep 2015
Initial Version
for Android
Evolution
Native
Oct 2015
v0.14.0
v0.13.0
ES6 support
Windows and Tizen are coming (Microsoft & Samsung)
Instagram
React Features
• Declarative APIs
• Immutable Data
• Virtual DOM
• JSX
• Component-Based
• Server Rendering (isomorphic)
• Learn Once, Write Anywhere
Imperative vs Declarative
How What
Imperative vs Declarative
• Go to kitchen
• Open fridge
• Remove chicken from fridge
• ***
• Bring food to table
• I want a dinner with chicken
Dinner
Imperative vs Declarative
React Component
HTML in javascript
CSS in javascript
Inline Style
Immutability
Shared mutable state is
the root of all evil
- Pete Hunt, React.js Conf 2015
7 Types in Javascript
Type Immutable
null Yes
undefined Yes
Boolean Yes
Number Yes
String Yes
Symbol Yes
Object No
7 Types in Javascript
shouldComponentUpdate
• PureRenderMixin
• Shallow Compare
Solutions
• Deep Clone
• Immutability Helpers
• Immutable.js
Immutable.js
Immutable.js
Redux
Dan Abramov
A predictable state container for javascript apps.
Redux
• Flux May 2014
• Re-frame Dec 2014
• Relay Feb 2015
• Redux May 2015
• Om/next July 2015
Current Solutions
Principle
• Single source for whole application
• State is Immutable (read-only)
• Changes are made with Pure Functions
(previousState, action) => newState
Pure Function
• Simplicity
• Fewer bugs
• Code re-use
• Easier to test
• High performance
https://en.wikipedia.org/wiki/Pure_function
e.g.
• sin(x)
• e => e * 2
Basic Concept
• Action
• Reducer
• Store
Redux Flow
React
Components
Store
Action
Creators
Reducers
e.g. onClick
dispatch(action) (previousState, action)
(newState)(state)
Strict unidirectional data flow
React + Redux
Action Creator
Reducer
GraphQL
A query language and execution engine
tied to any backend service.
GraphQL
GraphQL challenges REST
REST Issues
• Custom endpoint
• Over-fetching
• Multi versions for client
GraphQL Query
Query Response
Relay
Relay
Using GraphQL in React
A javascript framework for
building data-driven React Application
Relay Flow
React
Components
Relay
Store
Action
Server
GraphQl
Response Props
GraphQl
Write
GraphQl Query
Update
+ +
Is the future?
Thanks
Perfection is achieved
not when there is nothing more to add,
but when there is nothing left to take away.
- Tom Occhino

Best Practice-React