96

TPSE Thailand 2015 - Rethinking Web with React and Flux

Embed Size (px)

Citation preview

Page 1: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 2: TPSE Thailand 2015 - Rethinking Web with React and Flux

Rethinking Web AppWith React.JS and Flux

Page 3: TPSE Thailand 2015 - Rethinking Web with React and Flux

Rethinking Web AppWith React.JS and Flux

Page 4: TPSE Thailand 2015 - Rethinking Web with React and Flux

Rethinking Web AppWith React.JS and Flux

Page 5: TPSE Thailand 2015 - Rethinking Web with React and Flux

JIRAT KIJLERDPORNPAILOJ

NET

n3tr

Page 6: TPSE Thailand 2015 - Rethinking Web with React and Flux

Software Engineer - iOS, Javascript, Python

Page 7: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 8: TPSE Thailand 2015 - Rethinking Web with React and Flux

Web

17mDaily PV

App

14mDaily PV

Page 9: TPSE Thailand 2015 - Rethinking Web with React and Flux

Why a new web app?

Page 10: TPSE Thailand 2015 - Rethinking Web with React and Flux

Platform Maturity + Mobile FirstWhy a new web app?

Page 11: TPSE Thailand 2015 - Rethinking Web with React and Flux

Monolithic(WEB)

Page 12: TPSE Thailand 2015 - Rethinking Web with React and Flux

WEBBROWSER DATABASE

Page 13: TPSE Thailand 2015 - Rethinking Web with React and Flux

WEB

API

BROWSER

MOBILE APP

DATABASE

Page 14: TPSE Thailand 2015 - Rethinking Web with React and Flux

WEB

API

BROWSER

MOBILE APP

DATABASE

Page 15: TPSE Thailand 2015 - Rethinking Web with React and Flux

WEB

API

BROWSER

MOBILE APPDATABASE

! Direct connection

! Caching

! Caching

- Expensive works!!- Template- Large data transfer

!

- Cheap and fast!!- JSON Response- Small data transfer

!

Page 16: TPSE Thailand 2015 - Rethinking Web with React and Flux

SERVER

API

CLIENT

MOBILE APP

DATABASE

FrontendPlatform

Page 17: TPSE Thailand 2015 - Rethinking Web with React and Flux

Let’s see

How Web App Works

Page 18: TPSE Thailand 2015 - Rethinking Web with React and Flux

Sever Rendered Web App

PAGE SERVER DATABASEGET

HTML

Page 19: TPSE Thailand 2015 - Rethinking Web with React and Flux

• Pros• SEO Friendly• User perceive page load performance• Easy to develop and maintain

• Cons• Lack of User experience• Reload page on every next request

Sever Rendered Web App

Page 20: TPSE Thailand 2015 - Rethinking Web with React and Flux

Ajax Era

PAGESERVER DATABASE

AJAX

eg. contact form, load more, etc.

GET

JSON

Page 21: TPSE Thailand 2015 - Rethinking Web with React and Flux

Server Rendered + Ajax

• Pros• SEO (still) Friendly• Better Load Performance• Better UX

• Cons• Hard to develop and maintain

• Different in server-client script language

Page 22: TPSE Thailand 2015 - Rethinking Web with React and Flux

Single Page Web App (Fat Client)

PAGESERVER DATABASE

FAT JSFAT JS

GET

Page 23: TPSE Thailand 2015 - Rethinking Web with React and Flux

Single Page App (SPA)

• Pros• Best for User Experience• Maintenance (one or two languages)

• Cons• Load performance• Lack of SEO

Page 24: TPSE Thailand 2015 - Rethinking Web with React and Flux

SEO vs UX ?

Page 25: TPSE Thailand 2015 - Rethinking Web with React and Flux

SEO vs UX ?We need both

Page 26: TPSE Thailand 2015 - Rethinking Web with React and Flux

Wait - There is still hope

Page 27: TPSE Thailand 2015 - Rethinking Web with React and Flux

Hybrid Rendered Web

Page 28: TPSE Thailand 2015 - Rethinking Web with React and Flux

Hybrid Rendered

SERVERPAGE

2. HTML Content + FAT JS

1. GET

4. JS LOADED AND RERENDER

3. LOADING JS

(User see content)

Page 29: TPSE Thailand 2015 - Rethinking Web with React and Flux

Hybrid Rendered

SERVER

2. HTML Content + FAT JS

1. GET

4. JS LOADED AND RERENDER

3. LOADING JS

(User see content)

APP

Page 30: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 31: TPSE Thailand 2015 - Rethinking Web with React and Flux

ServerNode.js

PHPRuby

Python Go

JavaEtc

ClientJavascript

Page 32: TPSE Thailand 2015 - Rethinking Web with React and Flux

ServerNode.js

PHPRuby

Python Go

JavaEtc

ClientJavascript

Page 33: TPSE Thailand 2015 - Rethinking Web with React and Flux

PHP

Javascript

@foreach ($users as $user)

<p>This is user {{ $user->id }}</p>

@endforeach

{{#each users}}

<p>{{id}}</p>

{{/each}}

Template

Page 34: TPSE Thailand 2015 - Rethinking Web with React and Flux

PHP

Javascript

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "example.com");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

curl_close($ch);

request

.get('example.com')

.end(function(err, res){

});

Networking

Page 35: TPSE Thailand 2015 - Rethinking Web with React and Flux

Isomorphic Web Application

Page 36: TPSE Thailand 2015 - Rethinking Web with React and Flux

Javascript code that can be shared between envoriments

Page 37: TPSE Thailand 2015 - Rethinking Web with React and Flux

Why Isomorphic web app

- One launguage to rule them all (Javascript)- Reduce duplicated code and Increase reusable.- Server Rendered == SEO- Client Rendered == UX

Page 38: TPSE Thailand 2015 - Rethinking Web with React and Flux

Here comes IsomorhicClient

View

Utils

Router

Model

Server

View

Utils

Router

Model

Page 39: TPSE Thailand 2015 - Rethinking Web with React and Flux

Here comes IsomorhicClient

Utils

Model

Server

Router

ViewSome cannot be share

- Persistent

Page 40: TPSE Thailand 2015 - Rethinking Web with React and Flux

What are our options?

- Rendr.js- Angular.js- React.js

Page 41: TPSE Thailand 2015 - Rethinking Web with React and Flux

Angular.js

Page 42: TPSE Thailand 2015 - Rethinking Web with React and Flux

Angular.jsNot support server-side rendering

Page 43: TPSE Thailand 2015 - Rethinking Web with React and Flux

ReactSay hello to...

Page 44: TPSE Thailand 2015 - Rethinking Web with React and Flux

V in MVCnot a framework

Page 45: TPSE Thailand 2015 - Rethinking Web with React and Flux

React.js- Developed by Facebook- Designed for Isomorphic rendering

- Works on the server and the client with on simple API- Virtual DOM diff implementation for ultra-high performance- Component-based

- Reusable & Composable- When you scale, just build more modular components

- Testable (support Unit Tests)

Page 46: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 47: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 48: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 49: TPSE Thailand 2015 - Rethinking Web with React and Flux

React.js Component- Composable and Reusable- Data pass-in from Parent component called props- Data owned by component itself called state- Re-render whole component when state or props changed!!

Page 50: TPSE Thailand 2015 - Rethinking Web with React and Flux

var HelloComponent = React.createClass({

getInitialState: function(){

name: 'Net'

},

render: function() {

return <p>Hello, {this.state.name}</p>;

}

});

What it look like?

// <p>Hello, Net</p>

Page 51: TPSE Thailand 2015 - Rethinking Web with React and Flux

Render your component

React.render(<HelloComponent />, document.getElementById('example'));

var html = React.renderToString(<HelloComponent />);

// "<p>Hello, Net</p>"

Client

Server

Page 52: TPSE Thailand 2015 - Rethinking Web with React and Flux

Let’s build React - TodoAppTodoApp

TodoTextField

TodoList

TodoListItem

TodoListItem

TodoListItem

1. TodoApp2. TodoTextField3. TodoList4. TodoListItem

Page 53: TPSE Thailand 2015 - Rethinking Web with React and Flux

Demo

Page 54: TPSE Thailand 2015 - Rethinking Web with React and Flux

MVC

Page 55: TPSE Thailand 2015 - Rethinking Web with React and Flux

MVC is hard to Scale on webWhy?

Page 56: TPSE Thailand 2015 - Rethinking Web with React and Flux

Got a new message or friend go offline, what’s happen?

Page 57: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 58: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 59: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 60: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 61: TPSE Thailand 2015 - Rethinking Web with React and Flux

Remember TodoList

“We want History panel in TodoApp”

Page 62: TPSE Thailand 2015 - Rethinking Web with React and Flux

Okay, Let’s do it

onSubmitTodo: function(todo){

addTodo(todo);

}

Page 63: TPSE Thailand 2015 - Rethinking Web with React and Flux

Okay, Let’s do it

onSubmitTodo: function(todo){

addTodo(todo);

addHistory(todo); // Easy, Right?

}

Page 64: TPSE Thailand 2015 - Rethinking Web with React and Flux

What if we need more?

onSubmitTodo: function(todo){

addTodo(todo);

addHistory(todo); // Easy, Right?

}

Page 65: TPSE Thailand 2015 - Rethinking Web with React and Flux

What if we need more?

onSubmitTodo: function(todo){

addTodo(todo);

addHistory(todo);

doSomething(todo);

}

Page 66: TPSE Thailand 2015 - Rethinking Web with React and Flux

What if we need more? and more?

onSubmitTodo: function(todo){

addTodo(todo);

addHistory(todo);

doSomething(todo);

doMore(todo);

}

Page 67: TPSE Thailand 2015 - Rethinking Web with React and Flux

What if we need more? and more? ...

onSubmitTodo: function(todo){

addTodo(todo);

addHistory(todo);

doSomething(todo);

doMore(todo);

andMore(todo);

}

Page 68: TPSE Thailand 2015 - Rethinking Web with React and Flux

3 months later

We do not need History anymore, let’s remove it

Page 69: TPSE Thailand 2015 - Rethinking Web with React and Flux

3 months laterWe do not need History anymore, let’s remove it

Page 70: TPSE Thailand 2015 - Rethinking Web with React and Flux

FluxLet’s it

Page 71: TPSE Thailand 2015 - Rethinking Web with React and Flux

Flux & You- Developed by Facebook- Unidirectional Data Flow- Flux is Software Architecture not a framework or even Library

Page 72: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Page 73: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Store

Add Change Listener

Page 74: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Store

Dispatcher

Add Change Listener

Register for Event

Page 75: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Store

Dispatcher

Real-time update Form InputInitial Data

Add Change Listener

Register for Event

Page 76: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Store

Dispatcher

Real-time update Form InputInitial Data

Add Change Listener

Register for Event Dispatch

Page 77: TPSE Thailand 2015 - Rethinking Web with React and Flux

View

Store

Dispatcher

Real-time update Form InputInitial Data

Add Change Listener

Register for Event Dispatch

Emit

Page 78: TPSE Thailand 2015 - Rethinking Web with React and Flux

TodoView

HistoryView

TodoStore

TodoTextField

TodoList

TodoHistoryList

CREATE_TODOAction

HistoryStore

Dispatch

Dispatch

Page 79: TPSE Thailand 2015 - Rethinking Web with React and Flux

Flux TodoList and History Demo

Page 80: TPSE Thailand 2015 - Rethinking Web with React and Flux

No time to explain

Page 81: TPSE Thailand 2015 - Rethinking Web with React and Flux

Yahoo’s flux implement - support client and serverhttps://github.com/yahoo/fluxible

Fluxible

Page 82: TPSE Thailand 2015 - Rethinking Web with React and Flux

AirBnB’s Flux implement - support both server and renderhttps://github.com/goatslacker/alt

Alt.js

Page 83: TPSE Thailand 2015 - Rethinking Web with React and Flux

Ajax with less suckhttps://github.com/visionmedia/superagent

Superagent

Page 84: TPSE Thailand 2015 - Rethinking Web with React and Flux

Forget about it - a lot of better tool out there

jQuery

Page 85: TPSE Thailand 2015 - Rethinking Web with React and Flux

Module bundle - Grunt/Gulp replacementhttp://webpack.github.io/

Webpack

Page 86: TPSE Thailand 2015 - Rethinking Web with React and Flux

One more thing

Page 87: TPSE Thailand 2015 - Rethinking Web with React and Flux

Frontend Engineer

Page 88: TPSE Thailand 2015 - Rethinking Web with React and Flux

HTML + CSS + Javascript

Page 89: TPSE Thailand 2015 - Rethinking Web with React and Flux

HTML + CSS + Javascript

Page 90: TPSE Thailand 2015 - Rethinking Web with React and Flux
Page 91: TPSE Thailand 2015 - Rethinking Web with React and Flux

https://github.com/n3tr/TPSE-react-demo

Demo Code

Page 92: TPSE Thailand 2015 - Rethinking Web with React and Flux

https://github.com/n3tr/isomorphic-weather

Fluxible Example (Server+Client rendered)

Page 94: TPSE Thailand 2015 - Rethinking Web with React and Flux

Hacker Way: Rethinking Web App Development at Facebook

https://www.youtube.com/watch?v=nYkdrAPrdcw

Flux over MVC

Page 95: TPSE Thailand 2015 - Rethinking Web with React and Flux

Pete Hunt: React - Rethinking Best Practices (updated) - JSConf.Asia 2013

https://www.youtube.com/watch?v=DgVS-zXgMTk

This is show why they create React

Page 96: TPSE Thailand 2015 - Rethinking Web with React and Flux

OSCON 2014: How Instagram.com Works; Pete Hunt

https://www.youtube.com/watch?v=VkTCL6Nqm6Y

Why Webpack is Rocks!!