+ All Categories
Home > Internet > Functional Web Development

Functional Web Development

Date post: 01-Dec-2014
Category:
Upload: fitc
View: 178 times
Download: 0 times
Share this document with a friend
Description:
Functional Web Development – An Introduction to React.js with Bertrand Karerangabo Presented at FITC's Web Unleashed 2014 conference on September 18 2014 More info at www.fitc.ca 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. Bertrand Karerangabo will dive into those concepts that make React.js unique and along the way, also learn how to build web applications from simple, composable and reusable components. OBJECTIVE Rethink web development best practices and explore how you can build ambitious and performant application using functional programming with a virtual DOM representation. TARGET AUDIENCE Javascript developers working on medium to large dynamic applications. ASSUMED AUDIENCE KNOWLEDGE A solid understanding of Javascript and the DOM is strongly recommended. FIVE THINGS AUDIENCE MEMBERS WILL LEARN What React.js is and why it was built. How to deal with the “evil” of mutable state in non-trivial applications. A strategy for working around notoriously slow and expensive DOM operations. The way to truly separate concerns, instead of just technologies, in an application. The SEO, performance and usability benefits that come from using a client-side framework that plays nice with the server.
53
FUNCTIONAL WEB DEVELOPMENT - AN INTRODUCTION TO React.js
Transcript
Page 1: Functional Web Development

FUNCTIONAL WEB DEVELOPMENT - AN

INTRODUCTION TO React.js

Page 2: Functional Web Development

HELLO, MY NAME IS Bertrand KARERANGABO.

@BERTRANDK

Page 3: Functional Web Development

I WORK AT RANGLE.IO

Page 4: Functional Web Development

Moar functional!— Nick Van Weerdenburg

Page 5: Functional Web Development

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

Page 6: Functional Web Development

WHAT PROBLEM ARE WE TALKING ABOUT TODAY?

Page 7: Functional Web Development

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

THE PROBLEMHOW CAN WE BUILD LARGE APPLICATIONS

WITH DATA THAT CHANGES OVER TIME?

Page 9: Functional Web Development
Page 10: Functional Web Development

▸ 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

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

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

IMPLEMENTATION.

Page 13: Functional Web Development

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

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

1995 · THE FIRST JAVASCRIPT

Page 16: Functional Web Development

▸ 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

A SHOPPING CART

Page 18: Functional Web Development

A SHOPPING CART - OOP DATA

Page 19: Functional Web Development

A SHOPPING CART - OOP DATA & METHODS

Page 20: Functional Web Development

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

Page 21: Functional Web Development

A BASIC COMPUTER IN 1995Ram: 8MB

HDD: 1GB

CPU: 33MHz

Page 22: Functional Web Development

LET'S NOT WRITE SOFTWARE LIKE

IT'S 1995.

Page 23: Functional Web Development

WE NEED A BETTER

ABSTRACTION

Page 24: Functional Web Development

▸ 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

A SHOPPING CART - FP DATA

Page 26: Functional Web Development

A SHOPPING CART - FP DATA

Page 27: Functional Web Development

A SOLUTIONREACT.JS + IMMUTABLE-JS

Page 28: Functional Web Development

▸ 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

REACT.JSA JAVASCRIPT LIBRARY FOR BUILDING

COMPOSABLE USER INTERFACES

Page 30: Functional Web Development

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

REACT.JS COMPONENTvar App = React.createClass({

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

});

Page 32: Functional Web Development

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

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

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

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

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

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

Page 38: Functional Web Development

REACT.JS COMPONENT LIFECYCLEMounting: - componentWillMount - componentDidMount

Updating: - componentWillReceiveProps - shouldComponentUpdate - componentWillUpdate - componentDidUpdate

Unmounting: - componentWillUnmount

Page 39: Functional Web Development

REACT.JS SHOPPING CART

Page 40: Functional Web Development

▸ 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

IMMUTABLEIMMUTABLE AND PERSISTENT DATA STRUCTURES + SUPPORTING APIS

Page 42: Functional Web Development

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

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

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

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

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

IMMUTABLE SHOPPING CART

Page 48: Functional Web Development

▸ 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

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

▸ 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

WE NOW HAVE SOLID MODEL FOR DATA THAT CHANGES

OVER TIME.

Page 52: Functional Web Development

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

Thank you!


Recommended