Functional Web Development – An Introduction to React.js

53
FUNCTIONAL WEB DEVELOPMENT - AN INTRODUCTION TO React.js

description

A presentation by Bertrand Karerangabo from rangle.io at the FITC Web Unleashed. React.js is a UI framework created by Facebook and Instagram. Its primary design goal is to help build large applications with data that changes over time. To do so at scale, conventional wisdom and some long-held assumptions about software development had to be challenged. Gone are the “M” and the “C” in MVC. Gone are templates and special HTML directives. Gone also are traditional data-bindings. The results are applications that are extremely fast and reliable, out of the box.

Transcript of Functional Web Development – An Introduction to React.js

Page 1: Functional Web Development – An Introduction to React.js

FUNCTIONAL WEB DEVELOPMENT - AN

INTRODUCTION TO React.js

Page 2: Functional Web Development – An Introduction to React.js

HELLO, MY NAME IS Bertrand KARERANGABO.

@BERTRANDK

Page 3: Functional Web Development – An Introduction to React.js

I WORK AT RANGLE.IO

Page 4: Functional Web Development – An Introduction to React.js

Moar functional!— Nick Van Weerdenburg

Page 5: Functional Web Development – An Introduction to React.js

typeof NaN === 'number' //=> true

Page 6: Functional Web Development – An Introduction to React.js

WHAT PROBLEM ARE WE TALKING ABOUT TODAY?

Page 7: Functional Web Development – An Introduction to React.js

Most software today is very much like an Egyptian

pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands

of slaves.— Alan Kay

Page 8: Functional Web Development – An Introduction to React.js

THE PROBLEMHOW CAN WE BUILD LARGE APPLICATIONS

WITH DATA THAT CHANGES OVER TIME?

Page 9: Functional Web Development – An Introduction to React.js
Page 10: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 11: Functional Web Development – An Introduction to React.js

1979 · MODEL-VIEW-CONTROLLER IS BORNIt was fist articulated by Trygve Reenskaug, Adele Goldberg and others

at Xeroc PARC.

MVC was conceived as a general solution to the problem of users controlling a large and complex data set.

Page 12: Functional Web Development – An Introduction to React.js

MVC AND IT'S LATER VARIANTS HAVE ALL INHERITED FROM THE INITIAL OOP-BASED

IMPLEMENTATION.

Page 13: Functional Web Development – An Introduction to React.js

1990 · THE FIRST WEB BROWSER

It was invented by Tim Berners-Lee and was initially called WorldWideWeb.

▸ It was written in Objective-C.▸ It introduced an Internet-based document

system called HyperText Markup Language.▸ The layout engine was a subclass of

NeXTSTEP's Text class.▸ The document is static – if data changes, you

must refresh!

Page 14: Functional Web Development – An Introduction to React.js

1995 · THE FIRST DOMThe Level 0 DOM was created by Netscape for Netscape Navigator 2.0.

The idea was to give web developers a means by which to allow users to interact with a site.

Given that an HTML document can be represented as a tree, the DOM API allows for developers to interact and manipulate the tree's nodes.

Page 15: Functional Web Development – An Introduction to React.js

1995 · THE FIRST JAVASCRIPT

Page 16: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 17: Functional Web Development – An Introduction to React.js

A SHOPPING CART

Page 18: Functional Web Development – An Introduction to React.js

A SHOPPING CART - OOP DATA

Page 19: Functional Web Development – An Introduction to React.js

A SHOPPING CART - OOP DATA & METHODS

Page 20: Functional Web Development – An Introduction to React.js

Local state that changes over time is the root of all evil.

Page 21: Functional Web Development – An Introduction to React.js

A BASIC COMPUTER IN 1995Ram: 8MB

HDD: 1GB

CPU: 33MHz

Page 22: Functional Web Development – An Introduction to React.js

LET'S NOT WRITE SOFTWARE LIKE

IT'S 1995.

Page 23: Functional Web Development – An Introduction to React.js

WE NEED A BETTER

ABSTRACTION

Page 24: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 25: Functional Web Development – An Introduction to React.js

A SHOPPING CART - FP DATA

Page 26: Functional Web Development – An Introduction to React.js

A SHOPPING CART - FP DATA

Page 27: Functional Web Development – An Introduction to React.js

A SOLUTIONREACT.JS + IMMUTABLE-JS

Page 28: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 29: Functional Web Development – An Introduction to React.js

REACT.JSA JAVASCRIPT LIBRARY FOR BUILDING

COMPOSABLE USER INTERFACES

Page 30: Functional Web Development – An Introduction to React.js

REACT.JS VIRTUAL DOMThe full DOM API in JavaScript.

When rendering, it uses a diff implementation for ultra-high performance.

f(newDOM, oldDOM) = Δ

Page 31: Functional Web Development – An Introduction to React.js

REACT.JS COMPONENTvar App = React.createClass({

render: function() { return React.DOM.h1(null, 'Hello'); };

});

Page 32: Functional Web Development – An Introduction to React.js

REACT.JS RENDERvar App = React.createClass({

render: function() { return React.DOM.h1(null, 'Hello'); };

});

React.renderComponent(App(null), document.body);

Page 33: Functional Web Development – An Introduction to React.js

REACT.JS PROPSvar french = 'Allo';

var App = React.createClass({

render: function() { return React.DOM.h1(null, this.props.salutation ); };

});

React.renderComponent(App({ salutation: french }), document.body);

Page 34: Functional Web Development – An Introduction to React.js

REACT.JS DOM PROPERTIESvar french = 'Allo';

var App = React.createClass({

render: function() { return React.DOM.h1({ className: 'title' }, this.props.salutation ); };

});

React.renderComponent(App({ salutation: french }), document.body);

Page 35: Functional Web Development – An Introduction to React.js

REACT.JS EVENTSvar french = 'Allo';

function scream() { alert("I've been clicked!");}

var App = React.createClass({

render: function() { return React.DOM.h1({ className: 'title', onClick: this.props.clickHandler }, this.props.salutation ); };

});

React.renderComponent(App({ salutation: french, clickHandler: scream }), document.body);

Page 36: Functional Web Development – An Introduction to React.js

REACT.JS STATE*var french = 'Allo';

var App = React.createClass({ getInitialState: function() { return { name: 'Unknown' }; }, componentDidMount: function() { $.ajax({ url: '/me', dataType: 'json', success: function(data) { this.setState({ name: data }); }.bind(this); }); }, render: function() { var fullSalutation = this.props.salutation + ', ' + this.state.name;

return React.DOM.h1(null, fullSalutation); };

});

React.renderComponent(App({ salutation: french }), document.body);

Page 37: Functional Web Development – An Introduction to React.js

REACT.JS COMPONENT SPECIFICATION- render- getInitialState- getDefaultProps- propTypes- mixins- statics- displayName

Page 38: Functional Web Development – An Introduction to React.js

REACT.JS COMPONENT LIFECYCLEMounting: - componentWillMount - componentDidMount

Updating: - componentWillReceiveProps - shouldComponentUpdate - componentWillUpdate - componentDidUpdate

Unmounting: - componentWillUnmount

Page 39: Functional Web Development – An Introduction to React.js

REACT.JS SHOPPING CART

Page 40: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 41: Functional Web Development – An Introduction to React.js

IMMUTABLEIMMUTABLE AND PERSISTENT DATA STRUCTURES + SUPPORTING APIS

Page 42: Functional Web Development – An Introduction to React.js

IMMUTABLE MAPSvar map1 = Immutable.Map({ a: 1, b: 2, c: 3 });

map2 = map.set('b', 20); // note the assignmentmap2.get('b'); // 20

map1.get('b'); // 2

Page 43: Functional Web Development – An Introduction to React.js

IMMUTABLE VECTORvar vect1 = Immutable.Vector(1, 2);var vect2 = vect1.push(3, 4, 5);var vect3 = vect2.unshift(0);var vect4 = vect1.concat(vect2, vect3);assert(vect1.length === 2);assert(vect2.length === 5);assert(vect3.length === 6);assert(vect4.length === 13);assert(vect4.get(0) === 1);

Page 44: Functional Web Development – An Introduction to React.js

IMMUTABLE EQUALITYvar map1 = Immutable.Map({a:1, b:1, c:1});var map2 = Immutable.Map({a:1, b:1, c:1});assert(map1 !== map2);assert(Immutable.is(map1, map2) === true);

Page 45: Functional Web Development – An Introduction to React.js

IMMUTABLE INTEROPvar deep = Immutable.Map({a:1, b:2, c:Immutable.Vector(3,4,5)});deep.toObject() // { a: 1, b: 2, c: Vector [ 3, 4, 5 ] }deep.toArray() // [ 1, 2, Vector [ 3, 4, 5 ] ]deep.toJS() // { a: 1, b: 2, c: [ 3, 4, 5 ] }JSON.stringify(deep) // '{"a":1,"b":2,"c":[3,4,5]}'

Page 46: Functional Web Development – An Introduction to React.js

IMMUTABLE CURSORSvar appData = Immutable.fromJS({ a: { b: { c: 1 } } });var userData = appData.cursor(['a', 'b', 'c'], function(newData) { appData = newData;});

// ... elsewhere ...

userData.deref(); // 1userData = userData.update(function(x) { return x + 1 });userData.deref(); // 2

// ... back to data ...

appData.getIn(['a', 'b', 'c']); // 2

Page 47: Functional Web Development – An Introduction to React.js

IMMUTABLE SHOPPING CART

Page 48: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 49: Functional Web Development – An Introduction to React.js

SERVER-SIDE RENDERINGvar url = require('url')

function(req, res, next) { // get the application path/stage var path = url.parse(req.url).pathname;

// get a React.js component var app = App({path: path});

// render component into string instead of DOM var markup = React.renderComponentToString(app);

// return full html and let client-side takeover res.send(markup);}

Page 50: Functional Web Development – An Introduction to React.js

▸ A (very) short history of web development▸ OOP in the wild▸ Functional design▸ React.js

▸ Immutable (data stuctures)▸ The server (SEO & performance)

▸ The end

Page 51: Functional Web Development – An Introduction to React.js

WE NOW HAVE SOLID MODEL FOR DATA THAT CHANGES

OVER TIME.

Page 52: Functional Web Development – An Introduction to React.js

MIND THE PUNCHLINEIt's not important that the chicken crossed the road or even how it did

it.

What's important is why it crossed the road.

Page 53: Functional Web Development – An Introduction to React.js

Thank you!