Top Banner
REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYOND THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYON THE BROWSER REACT.JS: BEYO THE BROWSER REACT.JS: BEYO THE BROWSER REACT.JS: BEYO REACT.JS: BEYO REACT.JS: BEYO REACT.JS: BEYO REACT.JS: BEYO REACT.JS: BEY REACT.JS: BEYOND THE BROWSER @gabescholz
70

React.js: Beyond the Browser

Aug 10, 2015

Download

Technology

garbles
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: React.js: Beyond the Browser

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND THE BROWSER

REACT.JS: BEYOND REACT.JS: BEYOND REACT.JS: BEYOND REACT.JS: BEYOND REACT.JS: BEYOND

REACT.JS: BEYOND THE BROWSER

@gabescholz

Page 2: React.js: Beyond the Browser
Page 3: React.js: Beyond the Browser

“just remember I am going after you” - Godfrey Chan, wannabe Thought Leader

Page 4: React.js: Beyond the Browser

“the whole reason I am doing the talk is to go after you”

Page 5: React.js: Beyond the Browser
Page 6: React.js: Beyond the Browser

JAVASCRIPT

Page 7: React.js: Beyond the Browser

“… the latest and greatest JavaScript framework comes around every sixteen minutes.” - Allen Pike, King of VanJS

http://www.allenpike.com/2015/javascript-framework-fatigue/

Page 8: React.js: Beyond the Browser

“Helping you select an MV* framework”

!

(with 64 different choices!!)

http://todomvc.com/

Page 9: React.js: Beyond the Browser

CHOICE PARALYSIS /noun/ !

!

the state of being unable to select a proper JavaScript framework !

“I literally can’t feel my legs due to this choice paralysis.”

http://www.sitepoint.com/drowning-in-tools-web-development-industry/

Page 10: React.js: Beyond the Browser

“… people come from a wide variety of backgrounds, and have a wide variety of goals. This constant inflow of new ideas and interests has made the JavaScript community unusually diverse.”

http://www.allenpike.com/2015/javascript-framework-fatigue/

Page 11: React.js: Beyond the Browser

FOAM“FOAM is a full-stack, reactive, deeply MVC metaprogramming

Javascript framework.”<meta name=“description” … />

Page 12: React.js: Beyond the Browser
Page 13: React.js: Beyond the Browser
Page 14: React.js: Beyond the Browser
Page 15: React.js: Beyond the Browser

@gabescholz

Page 16: React.js: Beyond the Browser

e

Page 17: React.js: Beyond the Browser
Page 18: React.js: Beyond the Browser
Page 19: React.js: Beyond the Browser
Page 20: React.js: Beyond the Browser
Page 21: React.js: Beyond the Browser

2. “Learn once, Write anywhere”

1. Why React?

Page 22: React.js: Beyond the Browser

😍 Everything is JavaScript

Page 23: React.js: Beyond the Browser

<!-- template.html --> !<weather-widget forecast=“forecast”> <weather-link href=“http://weather.ca”> sherr, bud </weather-link> </weather-widget>

Page 24: React.js: Beyond the Browser

// component.js !<WeatherWidget forecast={forecast}> <WeatherLink href=“http://weather.ca”> sherr, bud </WeatherLink> </WeatherWidget>

Page 25: React.js: Beyond the Browser

// component.js !React.createElement(WeatherWidget, { forecast: forecast }, React.createElement(WeatherLink, { href: “http://weather.ca” }, “sherr, bud” ) )

Dat JSX, tho

Page 26: React.js: Beyond the Browser

// component.js !React.createElement(“div”, {}, React.createElement(“a”, { href: “http://weather.ca” }, “sherr, bud” ) )

Dat JSX, tho

FEELS GOOD MAN

Page 27: React.js: Beyond the Browser

var WeatherApp = React.createClass({ getInitialState () { return { forecast: ‘sunny’ }; }, render () { var forecast = this.state.forecast; ! return ( <WeatherWidget forecast={forecast}> <WeatherLink href=“http://weather.ca”> sherr, bud </WeatherLink> </WeatherWidget> ) } });

Page 28: React.js: Beyond the Browser

😁 Virtual DOM +

Diffing

Page 29: React.js: Beyond the Browser
Page 30: React.js: Beyond the Browser

renderA: <div /> renderB: <span />

[removeNode <div />], [insertNode <span />]

Page 31: React.js: Beyond the Browser

renderA: <PuttyPatrol /> renderB: <Goldar />

[removeNode <PuttyPatrol />], [insertNode <Goldar />]

Page 32: React.js: Beyond the Browser

renderA: <div id="ahHhhhHh" /> renderB: <div id=“after-10000-years-im-free” />

[replaceAttribute id "after-10000-years-im-free"]

Page 33: React.js: Beyond the Browser

😁 One-way Reactive Rendering

Page 34: React.js: Beyond the Browser

STATE vs PROPS in TWO MINUTES

Page 35: React.js: Beyond the Browser
Page 36: React.js: Beyond the Browser
Page 37: React.js: Beyond the Browser
Page 38: React.js: Beyond the Browser
Page 39: React.js: Beyond the Browser
Page 40: React.js: Beyond the Browser
Page 41: React.js: Beyond the Browser
Page 42: React.js: Beyond the Browser

var Parent = React.createClass({ // ... render () { return ( <div> <Son name={this.state.sonsName} /> <Daughter name={this.state.daughtersName} /> </div> ); } }); !var Daughter = React.createClass({ // ... render () { return ( <div> <div>{this.props.name}</div> <div>{this.state.favouriteColor}</div> <Dog name=`Mini ${this.props.name}` /> </div> ); } });

Page 43: React.js: Beyond the Browser

var Parent = React.createClass({ // … ! updateToLaquesha () { this.setState({ daughtersName: “Laquesha” }); }, ! render () { return ( <div> <button onClick={this.updateToLaquesha} /> <Son name={this.state.sonsName} /> <Daughter name={this.state.daughtersName} /> </div> ); } });

Page 44: React.js: Beyond the Browser

Going beyond the DOM

Page 45: React.js: Beyond the Browser

React Canvas

Created by Flipboard

60 fps on mobile browsers

Last commit ~1 week after React Native released publicly

Page 46: React.js: Beyond the Browser

<Surface> <Layer> <Text style={{ fontFace: ‘Calibri’, fontSize: ‘40pt’ }}>{this.state.text}</Text> </Layer> </Surface>

`

var context = canvas.getContext(‘2d’); context.font = '40pt Calibri'; context.fillText(text, 0, 0);

Page 47: React.js: Beyond the Browser

React Native

Created by Facebook

All of your business logic is written and runs in JavaScript using JavaScriptCore on iOS

UI Kit instead of DOM Nodes

Page 48: React.js: Beyond the Browser

<div> <img src=“http://i.imgur.com/OBB7tLg.gif” /> <input type=“text” /> <span>sherrr, bud</span> </div>

<View> <Image source={{uri: “http://i.imgur.com/OBB7tLg.gif”}} /> <TextInput /> <Text>sherrr, bud</Text> </View>

React Native

*style required but not included

Page 49: React.js: Beyond the Browser

ReactGibbon

Created by Netflix

Runs on their Gibbon platform

Renders JSON instead of DOM

Page 50: React.js: Beyond the Browser

`

I SHOULD PUT REACT ON SOMETHINGI SHOULD PUT REACT ON SOMETHING

Page 51: React.js: Beyond the Browser

https://github.com/garbles/react-pebble-demo

Page 52: React.js: Beyond the Browser

var card = UI.Card({ title: “Fetching movies list”, subtitle: “Just a minute..”, body: “1 sec..” }); card.show(); // ... card.hide(); !!!!!!!<Card title=“Fetching movies list” subtitle=“Just a minute..” body=“1 sec..” />

Page 53: React.js: Beyond the Browser

var list = UI.List(); list.sections([{}]); // for every item var items = list.items(0); list.items(0, items.concat(item)); !list.show(); // ... list.hide(); !!!!!<List onSelect={this.handleSelect}> <Item title=“..” ... /> <Item title=“..” ... /> <Item title=“..” ... /> <Item title=“..” ... /> </List>

Page 54: React.js: Beyond the Browser

var App = React.createClass({ getInitialState () { }, selectItem (item) { }, ! componentDidMount () { ajax(apiUrl, (response) => { this.setState({ movies: response[1].movies }) }); }, ! render () { var movies = this.state.movies.map( movie => { return (<Item title={movie.title} data={movie} />); }); ! if (movies.length) { return <List onSelect={this.selectItem}>{movies}</List>; } else { return <Card ... />; } } });

Page 55: React.js: Beyond the Browser

var App = React.createClass({ getInitialState () { return { movies: [] } }, selectItem (item) { }, ! componentDidMount () { ajax(apiUrl, (response) => { this.setState({ movies: response[1].movies }) }); }, ! render () { var movies = this.state.movies.map( movie => { return (<Item title={movie.title} data={movie} />); }); ! if (movies.length) { return <List onSelect={this.selectItem}>{movies}</List>; } else { return <Card ... />; } } });

Page 56: React.js: Beyond the Browser

var App = React.createClass({ getInitialState () { }, selectItem (item) { }, ! componentDidMount () { ajax(apiUrl, (response) => { this.setState({ movies: response[1].movies }) }); }, ! render () { var movies = this.state.movies.map( movie => { return (<Item title={movie.title} data={movie} />); }); ! if (movies.length) { return <List onSelect={this.selectItem}>{movies}</List>; } else { return <Card ... />; } } });

Page 57: React.js: Beyond the Browser

var App = React.createClass({ getInitialState () { }, selectItem (item) { }, ! componentDidMount () { ajax(apiUrl, (response) => { this.setState({ movies: response[1].movies }) }); }, ! render () { var movies = this.state.movies.map( movie => { return (<Item title={movie.title} data={movie} />); }); ! if (movies.length) { return <List onSelect={this.selectItem}>{movies}</List>; } else { return <Card ... />; } } });

Page 58: React.js: Beyond the Browser

😛 Becoming a React trickster

Page 59: React.js: Beyond the Browser

function createComponent(name) { ! var CustomComponent = function(props) { this.node = new PebbleNode(); this.subscriptions = null; this.listeners = null; this._mountImage = null; this._renderedChildren = null; }; ! CustomComponent.displayName = name; ! for (var i = 1, l = arguments.length; i < l; i++) { assign(CustomComponent.prototype, arguments[i]); } ! return CustomComponent; }

Page 60: React.js: Beyond the Browser

function createComponent(name) { ! var CustomComponent = function(props) { this.node = new PebbleNode(); this.subscriptions = null; this.listeners = null; this._mountImage = null; this._renderedChildren = null; }; ! CustomComponent.displayName = name; ! for (var i = 1, l = arguments.length; i < l; i++) { assign(CustomComponent.prototype, arguments[i]); } ! return CustomComponent; }

Page 61: React.js: Beyond the Browser

function createComponent(name) { ! var CustomComponent = function(props) { this.node = new PebbleNode(); this.subscriptions = null; this.listeners = null; this._mountImage = null; this._renderedChildren = null; }; ! CustomComponent.displayName = name; ! for (var i = 1, l = arguments.length; i < l; i++) { assign(CustomComponent.prototype, arguments[i]); } ! return CustomComponent; }

Page 62: React.js: Beyond the Browser

var Card = createComponent( ‘Card’, NodeMixin, ComponentMixin, ..., { /* additional behaviours */ } ); !var PebbleApp = React.createClass({ render () { return ( <Card title=“Hello World!”/> ); } }); !renderPebble(<PebbleApp />);

Page 63: React.js: Beyond the Browser

var NodeMixin = { putEventListener (...) { /* ... */ }, handleEvent (...) { /* ... */ }, destroyEventListeners (...) { /* ... */ }, applyNodeProps (...) { /* ... */ } }; !var ContainerMixin = assign({}, ReactMultiChild.Mixin, { moveChild (...) { /* ... */ }, createChild (...) { /* ... */ }, replaceChild (...) { /* ... */ }, updateChildrenAtRoot (...) { /* ... */ }, mountAndInjectChildren (...) { /* ... */ } }); !// per component { construct (...) { /* ... */ }, mountComponent (...) { /* ... */ }, unmountComponent (...) { /* ... */ }, receiveComponent (...) { /* ... */ } };

Page 64: React.js: Beyond the Browser

var Card = createComponent(‘Card’, NodeMixin, { construct (element) { this._currentElement = element; this.__instance = new UI.Card(); }, mountComponent (rootID, transaction, context) { var props = this._currentElement.props; this._rootNodeID = rootID; ! this.__instance.prop(props); this.__instance.show(); ! return this.node; }, unmountComponent () { this.__instance.hide(); }, receiveComponent (element) { this._currentElement = element; this.__instance.prop(element.props); } });

Page 65: React.js: Beyond the Browser

var Card = createComponent(‘Card’, NodeMixin, { construct (element) { this._currentElement = element; this.__instance = new UI.Card(); }, mountComponent (rootID, transaction, context) { var props = this._currentElement.props; this._rootNodeID = rootID; ! this.__instance.prop(props); this.__instance.show(); ! return this.node; }, unmountComponent () { this.__instance.hide(); }, receiveComponent (element) { this._currentElement = element; this.__instance.prop(element.props); } });

Page 66: React.js: Beyond the Browser

var Card = createComponent(‘Card’, NodeMixin, { construct (element) { this._currentElement = element; this.__instance = new UI.Card(); }, mountComponent (rootID, transaction, context) { var props = this._currentElement.props; this._rootNodeID = rootID; ! this.__instance.prop(props); this.__instance.show(); ! return this.node; }, unmountComponent () { this.__instance.hide(); }, receiveComponent (element) { this._currentElement = element; this.__instance.prop(element.props); } });

Page 67: React.js: Beyond the Browser

var Card = createComponent(‘Card’, NodeMixin, { construct (element) { this._currentElement = element; this.__instance = new UI.Card(); }, mountComponent (rootID, transaction, context) { var props = this._currentElement.props; this._rootNodeID = rootID; ! this.__instance.prop(props); this.__instance.show(); ! return this.node; }, unmountComponent () { this.__instance.hide(); }, receiveComponent (element) { this._currentElement = element; this.__instance.prop(element.props); } });

Page 68: React.js: Beyond the Browser

var Card = createComponent(‘Card’, NodeMixin, { construct (element) { this._currentElement = element; this.__instance = new UI.Card(); }, mountComponent (rootID, transaction, context) { var props = this._currentElement.props; this._rootNodeID = rootID; ! this.__instance.prop(props); this.__instance.show(); ! return this.node; }, unmountComponent () { this.__instance.hide(); }, receiveComponent (element) { this._currentElement = element; this.__instance.prop(element.props); } });

Page 69: React.js: Beyond the Browser

Resources: !https://docs.google.com/presentation/d/1afMLTCpRxhJpurQ97VBHCZkLbR1TEsRnd3yyxuSQ5YY/edit#slide=id.g380053cce_179 !https://fivejs.codeschool.com/episodes/72-episode-65-march-5th-2015/stories/463-framework-fatigue !http://www.sitepoint.com/drowning-in-tools-web-development-industry/ !http://www.allenpike.com/2015/javascript-framework-fatigue/ !http://www.todomvc.com !Prior Art: !https://github.com/reactjs/react-art !https://github.com/Flipboard/react-canvas !https://github.com/facebook/react !https://github.com/facebook/react-native

Page 70: React.js: Beyond the Browser

THANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKSTHANKS