42
0 React/Redux/Redux-Saga +サーバーサイドレンダリング エスキュービズム・テクノロジー Jun 17,2016 S-cubism Technology Inc.

React Redux Redux-Saga + サーバサイドレンダリング

  • Upload
    -

  • View
    5.569

  • Download
    12

Embed Size (px)

Citation preview

Page 1: React Redux Redux-Saga + サーバサイドレンダリング

0

React/Redux/Redux-Saga+サーバーサイドレンダリング

エスキュービズム・テクノロジー

Jun 17,2016S-cubism Technology Inc.

Page 2: React Redux Redux-Saga + サーバサイドレンダリング

私のJavaScript遍歴(フレームワーク)

1

Prototype.js jQuery素 Backbone.js Knockout.js

1年のブランク

React Redux Redux-Saga

時代は変わっていた!

Page 3: React Redux Redux-Saga + サーバサイドレンダリング

私のJavaScript遍歴(文法)

CoffeeScript ES5素 ES6 ES7

Page 4: React Redux Redux-Saga + サーバサイドレンダリング

https://facebook.github.io/react/

Page 5: React Redux Redux-Saga + サーバサイドレンダリング

古き良きWEBシステム

URL

HTML

サーバー ブラウザ

HTMLから”DOMツリー”を構築(DOM:Document Object Model)

Page 6: React Redux Redux-Saga + サーバサイドレンダリング

ブラウザでの動的な処理

#1

#2

#3

#4

#5

jQuery(“#2”).after(jQuery(“#5”))

複数の操作が同時に発生したら?↓

DOMの状態管理が難しかった

DOM

Page 7: React Redux Redux-Saga + サーバサイドレンダリング

手続き的から宣言的な処理へ

#1

#2

#3

#4

#1

#2

#5

#3

#4

state = [1, 2, 3, 4] state = [1, 2, 5, 3, 4] こうあるべきを定義

あとはよしなにDOMを構築してくれる

DOM DOM

Page 8: React Redux Redux-Saga + サーバサイドレンダリング

仮想DOMによる変更検知

#1

#2

#3

#4

#1

#2

#5

#3

#4

state = [1, 2, 3, 4] state = [1, 2, 5, 3, 4]

仮想DOM 仮想DOM

仮想DOMを構築し、変更箇所を検知↓

変更箇所だけ本来のDOMに反映

Page 9: React Redux Redux-Saga + サーバサイドレンダリング

Reactの文法(ES6版)

Page 10: React Redux Redux-Saga + サーバサイドレンダリング

コンポーネントからDOMを構築

http://code.tutsplus.com/tutorials/intro-to-the-react-framework--net-35660

ReactDOM.render

仮想DOMを経由

Page 11: React Redux Redux-Saga + サーバサイドレンダリング

データフロー

http://code.tutsplus.com/tutorials/intro-to-the-react-framework--net-35660

props props

props props

Page 12: React Redux Redux-Saga + サーバサイドレンダリング

コンポーネントのライフサイクル

props

state

親コンポーネントから与えられるデータ

内部で持つ状態

Mounted

Update

Unmounted

setState()

setProp()

props, stateの変更を検知して描画

render()

Page 13: React Redux Redux-Saga + サーバサイドレンダリング

Reactのコンパイル

http://www.pro-react.com/materials/appendixA/

汎用ビルドツール JS用コンパイラ

Page 14: React Redux Redux-Saga + サーバサイドレンダリング

https://github.com/reactjs/redux

Page 15: React Redux Redux-Saga + サーバサイドレンダリング

コンポーネント間で直接やり取り?

https://css-tricks.com/learning-react-redux/

Page 16: React Redux Redux-Saga + サーバサイドレンダリング

MVC?

https://www.codementor.io/reactjs/tutorial/intro-to-react-redux-pros

モデルの変更がどのビューに影響を与えるか予測が難しい

Page 17: React Redux Redux-Saga + サーバサイドレンダリング

https://css-tricks.com/learning-react-redux/

Reduxのデータフロー

Page 18: React Redux Redux-Saga + サーバサイドレンダリング

Reduxの全体像

http://chentsulin.github.io/redux-intro/

Page 19: React Redux Redux-Saga + サーバサイドレンダリング

タイマーの実装例

http://jaysoo.ca/2016/01/03/managing-processes-in-redux-using-sagas/

Page 20: React Redux Redux-Saga + サーバサイドレンダリング

アクションの定義

actions.js

http://jaysoo.ca/2016/01/03/managing-processes-in-redux-using-sagas/

Page 21: React Redux Redux-Saga + サーバサイドレンダリング

Reducerの実装

reducers.js

stateは直接編集せずコピーして編集する

Page 22: React Redux Redux-Saga + サーバサイドレンダリング

状態遷移図で表すと・・

http://jaysoo.ca/2016/01/03/managing-processes-in-redux-

using-sagas/

Page 23: React Redux Redux-Saga + サーバサイドレンダリング

Reactコンポーネントの実装

Page 24: React Redux Redux-Saga + サーバサイドレンダリング

Dispatch

timer.js

タイマースタート

描画

Subscribe

Page 25: React Redux Redux-Saga + サーバサイドレンダリング

ミドルウェア

loggerミドルウェアを使えば、以下のようなログを出力できる

http://chentsulin.github.io/redux-intro/

Page 26: React Redux Redux-Saga + サーバサイドレンダリング

Redux-Saga

Page 27: React Redux Redux-Saga + サーバサイドレンダリング

非同期のアクション

http://andrewhfarmer.com/react-ajax-best-practices/

Tick

Page 28: React Redux Redux-Saga + サーバサイドレンダリング

タイマーコンポーネント

状態遷移が伝わるまでに遅延がある

timer.js

ビューの中に非同期処理が入り混じる

Page 29: React Redux Redux-Saga + サーバサイドレンダリング

Redux-thunkミドルウェア

一つのアクションの中で非同期処理を記述タイマーはアクションの直後にスタート

actions.js

非同期処理の起点となるアクションが肥大化する

Page 30: React Redux Redux-Saga + サーバサイドレンダリング

Redux-Sagaミドルウェア

Page 31: React Redux Redux-Saga + サーバサイドレンダリング

STARTアクションが発生するまで処理は止まる

Generator

1秒待って、状態を参照

sagas.js

Page 32: React Redux Redux-Saga + サーバサイドレンダリング

責務の分離

Reducerの責務 → アクションに対する状態遷移

Sagaの責務 → 複雑非同期な処理の指揮

Page 33: React Redux Redux-Saga + サーバサイドレンダリング

もう少し複雑な例

http://yelouafi.github.io/redux-saga/docs/advanced/NonBlockingCalls.html

Page 34: React Redux Redux-Saga + サーバサイドレンダリング

authorizeを参照(forkはノンブロッキング)

認証リクエスト処理の切り出し

Page 35: React Redux Redux-Saga + サーバサイドレンダリング

補足:async/awaitとの違い

http://wecodetheweb.com/2016/01/23/handling-async-in-redux-with-sagas/

ES7でasync/awaitが提案されているが・・

ここのfetchは実行されてしまうredux-sagaではfetchの命令を待つことができる

Page 36: React Redux Redux-Saga + サーバサイドレンダリング

サーバーサイドレンダリング

Page 37: React Redux Redux-Saga + サーバサイドレンダリング

JSアプリのSEO問題

http://www.theseotailor.com.au/beginner-seo/introduction-to-seo/

Page 38: React Redux Redux-Saga + サーバサイドレンダリング

サーバーサイドの処理フロー

リクエストを受け取る

Reduxストアの初期化

ユニバーサルなルーティング

Redux-Sagaの起動

ユニバーサルなフェッチ

Reactコンポーネントの文字列化

Reduxストアの文字列化

レスポンスを返す

(React-Router)

(Redial)

Page 39: React Redux Redux-Saga + サーバサイドレンダリング

Redialを用いたユニバーサルなフェッチ

Page 40: React Redux Redux-Saga + サーバサイドレンダリング

サーバサイドとクライアントサイド両方で、以下のようにtriggerを実行する

Page 41: React Redux Redux-Saga + サーバサイドレンダリング

サンプルプロダクトの紹介

https://github.com/scubism/todo_center

Page 42: React Redux Redux-Saga + サーバサイドレンダリング

以上です