Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Flux and ReactJS
Search
Rei Colina
January 26, 2016
Programming
97
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Flux and ReactJS
An introduction to Flux and how to integrate ReactJS into it.
Rei Colina
January 26, 2016
Other Decks in Programming
See All in Programming
APNsからLive Activityを開始する話
yumnumm
0
170
Workers Cache を知る
syumai
0
360
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
280
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
35
17k
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
210
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
270
Domain-Driven Transformation
hschwentner
2
2.3k
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
Event is what happens
moro
2
780
SREの越境 / SRE Collaboration
y0hgi
2
310
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
660
Next.js: The final boss of Web Performance
guaca
0
260
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
BBQ
matthewcrist
90
10k
Embracing the Ebb and Flow
colly
88
5.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Evolving SEO for Evolving Search Engines
ryanjones
0
330
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
270
Skip the Path - Find Your Career Trail
mkilby
1
260
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
エンジニアに許された特別な時間の終わり
watany
109
250k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
600
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Transcript
flux Rei Colina @reinaldo_13 &react
Why?
?
Model Model Model Model Model Model Model Model
Flux!
- What is it? - Anatomy - Action - Dispatcher
- Store - View - The Big Picture - Libraries - Demo - Pros & Cons
What is Flux?
framework
library
Action Dispatcher Store View architectural pattern
Actions Dispatcher Stores Unidirectional Flow
Anatomy of Flux
Action Dispatcher Store View Application Structure
Action Dispatcher Store View { actionType: ‘create’, data: data }
function(data) { appDispatcher.dispatch( ); }
Action Dispatcher Store View var TodoActions = { create: function(text)
{ AppDispatcher.dispatch({ actionType: TodoConstants.TODO_CREATE, text: text }); }, updateText: function(id, text) { AppDispatcher.dispatch({ actionType: TodoConstants.TODO_UPDATE_TEXT, id: id, text: text }); } };
Action Dispatcher Store View .register(function callback) .dispatch(object action)
Action Dispatcher Store View var Dispatcher = require(‘flux’).Dispatcher; module.exports =
new Dispatcher();
- App state (private vars) - No setters; only getters
- Callbacks - Emits events on state change Action Dispatcher Store View
Action Dispatcher Store View var _todos = {};
Action Dispatcher Store View var TodoStore = assign({}, EventEmitter.prototype, {
getAll: function() { return _todos; }, emitChange: function() { this.emit(CHANGE_EVENT); }, addChangeListener: function(callback) { this.on(CHANGE_EVENT, callback); }, removeChangeListener: function(callback) { this.removeListener(CHANGE_EVENT, callback); } });
Action Dispatcher Store View AppDispatcher.register(function(action) { switch(action.actionType) { case TodoConstants.TODO_CREATE:
create(action.text); TodoStore.emitChange(); break; case TodoConstants.TODO_UPDATE_TEXT: update(action.id, {text: action.text}); TodoStore.emitChange(); break; default: // no op } });
Action Dispatcher Store View var CHANGE_EVENT = 'change'; var _todos
= {}; function create(text) { ... } function update(id, updates) { ... } var TodoStore = assign({}, EventEmitter.prototype, { getAll: function() { return _todos; }, emitChange: function() { this.emit(CHANGE_EVENT); }, addChangeListener: function(callback) { this.on(CHANGE_EVENT, callback); }, removeChangeListener: function(callback) { this.removeListener(CHANGE_EVENT, callback); } }); AppDispatcher.register(function(action) { switch(action.actionType) { case TodoConstants.TODO_CREATE: create(action.text); TodoStore.emitChange(); break; case TodoConstants.TODO_COMPLETE: update(action.id, {complete: true}); break; case TodoConstants.TODO_UPDATE_TEXT: update(action.id, {text: action.text}); TodoStore.emitChange(); break; default: // no op } }); module.exports = TodoStore;
- ReactJS Action Dispatcher Store View - Virtual DOM -
Composability - Predictable
Action Dispatcher Store View componentDidMount: function() { // add an
event listener TodoStore.addChangeListener(this._onChange); }, componentWillUnmount: function() { // remove the event listener TodoStore.removeChangeListener(this._onChange); }
Action Dispatcher Store View getInitialState: function() { // get data
from the store return { allTodos: TodoStore.getAll() }; }
Action Dispatcher Store View _onSave: function(text) { // change data
in the store TodoActions.updateText(id, text); }
Action Dispatcher Store View var TodoApp = React.createClass({ _onChange: function()
{ this.setState({ allTodos: TodoStore.getAll() }); }, _onSave: function(text) { TodoActions.updateText(id, text); } getInitialState: function() { return getTodoState(); }, componentDidMount: function() { TodoStore.addChangeListener(this._onChange); }, componentWillUnmount: function() { TodoStore.removeChangeListener(this._onChange); }, render: function() { return (<div> ... </div>); }, }); module.exports = TodoApp;
put it all together
The Flow Dispatcher Action Creators Store State View View Controller
View props props event Web Service
AJAX API Calls var _todos = {}; AppDispatcher.register(function(action) { switch(action.actionType)
{ case Constants.FETCH_TODOS: $.ajax({ url: '/todos/', success: function (data) { // update _todos } }); break; } }); From Stores Action Dispatcher Store View
AJAX API Calls var TodoActions = { fetchTodos: function(text) {
$.ajax({ url: '/todos/', success: function (data) { // dispatch an action } }); } }; From Actions Action Dispatcher Store View
The Flow Dispatcher Action Creators Store State View View Controller
View props props event Web Service
libraries
Implementation Libraries Flux Utils: https://github.com/facebook/flux Action Dispatcher View Store Store
Store
Implementation Libraries Flummox: https://github.com/acdlite/flummox Action View Store Store Store
Implementation Libraries Redux: https://github.com/rackt/redux Action Store View
the power of flux
None
framework
library
Action Dispatcher Store View architectural pattern
None
- Data is easy to follow - Stores as source
of truth - Testable - Boilerplate code - Easy to fall into anti-patterns
{eof} @reinaldo_13