“WRITE ONCE”
Multi-Client Code Sharing in ReactJS
IN THE BEGINNING…
Early 2014, choosing a client-side tech for new project
• jQuery + sweat?
• Backbone + handlebars + etc?
• Angular?
• Ember?
… how to choose … ?
THE INEXORABLE MARCH OF TECH
new, shiny
bleeding edge
mature & stable
graveyard
of debt
cliff of fail
WE TRIED REACT
~0.9
IS AWESOME
How do we get this paradigm going everywhere??
WHY SHARE CODE?
Constraints:
• time
• build
• research, choosing tools
• team
• cognitive overhead
Benefits:
• efficiency of time/team
• design & interaction consistency
across clients
THE JOURNEY
STEP 1: SEPARATING CONCERNS
Client-side Application [web & responsive]
├─┬─ UI component repo
│ ├─── interaction component #1
│ ├─── interaction component #2
│ └─── interaction component #3
├─┬─ our API library
│ ├─── HTTP API
│ └─── WSS API
└─── other dependencies
STEP 2: SHARE UI COMPONENTS
UI components
Client app Rails
[client][webpack]
STEP 2: SHARE UI COMPONENTS
UI components
Client app Rails
Gem
[server + client][webpack]
STEP 3: SHARE UI MORE
UI components
Client app Rails Native
[webview]
Gem
UI components
Desktop
STEP 4: SHARE UI EVEN MORE
Client app Rails Native
[webview]
Gem
UI components
Rails
Native
[react]
STEP 5: SHARE UI MORE BETTERER
Client app
Gem
DesktopiOS android
NATIVE COMPONENTS
MyComponent.ios.js
MyComponent.android.js
SHARING WEB + NATIVE
• Content apps, not rich interaction or device access:
• yes: facebook, instagram, gmail, meetup, linkedin, simple games
• no: snapchat, 1st-person shooters/runners
• JS object inline styles, not css
• Some context detection at the component level
WEB + NATIVE COMPONENTS
const el = Platform.OS === 'web'
? React.DOM.div
: React.View
...
render() {
return el({},'content')
}
UI components
Rails
Native
[react]
STEP 5: SHARE UI MORE BETTERER
Client app
Gem
DesktopiOS android
“LEARN A LOT, WRITE A LOT”
#codinglife

Writing Once: Multi-client code sharing in ReactJS