Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Redux: Flux Reduced
Search
sporto
August 31, 2015
Technology
1
320
Redux: Flux Reduced
sporto
August 31, 2015
Tweet
Share
More Decks by sporto
See All by sporto
React inside Elm
sporto
2
170
Elm
sporto
1
250
Practically Immutable
sporto
0
170
Webpack and React
sporto
4
380
Rails with Webpack
sporto
1
210
Lesson learnt building Single Page Application
sporto
0
110
Grunt
sporto
1
150
Safe Testing in Ruby
sporto
1
120
Go - A great language for building web applications
sporto
1
310
Other Decks in Technology
See All in Technology
【swonet.conf_】NOCメンバーが語るSTMの実態!! ~ShowNetから若者への贈り物~
shownet
PRO
0
310
SageMaker学習のツボ / The Key Points of Learning SageMaker
cmhiranofumio
0
160
Azure Verified Moduleを触って分かった注目ポイント/azure-verified-module-begin
mhrtech
1
390
リスクから学ぶKubernetesコンテナセキュリティ/k8s-risk-and-security
mochizuki875
1
320
見えづらい活動の成果の伝え方は日頃からめちゃくちゃ悩んでるけど、実際こんな取り組みをしな がら温度感を合わせにいってるよ / Conveying Hard-to-See Results
kakehashi
4
1.8k
Case Study: Concurrent Counting
ennael
PRO
0
110
Binary Hacks Rebooted 私選ハック集
nullpo_head
1
240
ラブグラフ紹介資料 〜プロダクト解体新書〜 / Lovegraph Product Deck
lovegraph
0
14k
普通の Web エンジニアのための様相論理入門 #yapcjapan / YAPC Hakodate 2024
ytaka23
5
1.4k
【shownet.conf_】多様化するネットワーク環境を柔軟に統合するルーティングテクノロジー
shownet
PRO
0
390
How CERN serves 1EB of data via FUSE
ennael
PRO
0
16k
【shownet.conf_】ShowNet 2024 ~ Inter * Network ~
shownet
PRO
0
520
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
The Cost Of JavaScript in 2023
addyosmani
43
5.9k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Web development in the modern age
philhawksworth
205
10k
From Idea to $5000 a Month in 5 Months
shpigford
381
46k
Code Reviewing Like a Champion
maltzj
519
39k
A Modern Web Designer's Workflow
chriscoyier
692
190k
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
Faster Mobile Websites
deanohume
304
30k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.9k
Clear Off the Table
cherdarchuk
91
320k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Transcript
Redux flux reduced @sebasporto
Flux • Made popular by Facebook for building React applications
• Old pattern (CQRS) with different naming for everything
Flux View Dispatcher Action (Event) Users Store Posts Store Action
(Event) Change Grab New state
Flux Orders Stats 1 $400 2 $100 ... Countries 1
Aus 2 Fra ... Aus $500 Fra $600 ... Today $ 100 ... • Makes state consistency across views much easier
Component Singleton Flux Store Dispatcher Message Get state
Server side rendering • Original flux, everything is a singleton
• Make SSR much harder • Each request needs its own universe
Server side rendering • Flux implementations without singletons started showing
up • Alt, Fluxible, Flummox, etc
Request arrives Render Tree Pass everything Response Server side rendering
Create users store Create orders store Create dispatcher
Problem • Too many things to instantiate and pass around
What if we only have one state object? Users store
Orders store Countries store Reduced to { users orders countries }
Redux only has one big state tree Less things to
create and pass around ORDERS COUNTRIES USERS STATE
Now we can just move the dispatcher inside the state
obj Dispatcher State Reduced to { dispatch, state: { users orders ... } }
Redux is only one object 04 State Dispatch Redux store
Render Tree Pass store Response Server side rendering Create redux
instance Request
Traditional flux case 'USERS_FETCH_SUCCESS': users = action.users state.push(users) break •
We manage the state on the stores
What if we just describe the transformations?
• Inspired by ELM updaters update action model = case
action of Increment -> model + 1 Decrement -> model - 1
function reducer(state, action) { switch(action.type) { case 'USERS_FETCH_SUCCESS': users =
action.users return state.merge(users) ... } } Reducers in Redux
Reducers in Redux • The expect you to return a
copy of the state, not mutate it • This is great for debugging • Seamless-immutable is great for this
The state is managed by Redux, you only describe the
transformations
redux Redux View dispatch Action (Event) state Change Grab New
state Reducers
Components of Redux
{ type: 'USERS_FETCH_SUCCESS' users: [...] } Action
function fetchSuccess(users) return { type: 'USERS_FETCH_SUCCESS' users: [...] } }
Action creator
const action = fetchSuccess(users) store.dispatch(action) Dispatch
function reducer(state, action) { switch(action.type) { case 'USERS_FETCH_SUCCESS': users =
action.users return state.merge(users) } } Reducer
Interacting with the outside world? e.g. ajax
redux View dispatch Async Action state Change Reducers Async Action
creators Async action Request Response Action Run async action
function fetch() return function(dispatch) { doSomeAjax(...) .then(function(response) { const successAction(response.data)
dispatch(successAction) } } } Async Action creators
Middleware Middleware redux View dispatch Action Action
Middleware • Logging • Stoping unnecessary request • Async actions
• Dev tools 04 03 02 01
redux View dispatch Action state Change Reducers Redux is really
simple
Not React specific anymore • Just an event and state
management library • Can be used with other things e.g. ng-redux
Interacting directly with the store import { createStore } from
'redux' const store = createStore(reducers) store.getState() store.subscribe(listener) store.dispatch(action)
With React import { createStore } from 'redux' import Provider
from 'react-redux' const store = createStore({...}) React.render(( <Provider store={store}> {() => <App />} </Provider> ), mountNode);
Other cool stuff
Hot reloading • Works quite well most of the time
• Will keep state intact
Dev tools
Time travel
Redux-crud Building CRUD apps? • Standard actions creators and reducers
for CRUD apps • https://github.com/Versent/redux-crud
Thanks