Slide 1

Slide 1 text

REACT  MEETUP  HAMBURG   FLUX  INTRODUCTION   Nils  Hartmann  (nils@nilshartmann)  

Slide 2

Slide 2 text

REACT  MEETUP  HAMBURG   WHY  FLUX?   Some  background  first...  

Slide 3

Slide 3 text

CommunicaNon   •  Down  via  proper,es   •  Up  via  callbacks     Top-­‐level  component   •  Works  on  single  model   •  (whatever  a  ‚model‘  is)   SINGLE  COMPONENT  HIERARCHIE   Component 3 Component 2 Component 1 props props callback Model (whatever that means)

Slide 4

Slide 4 text

Isolated  Models  with  independent  component  trees   •  Each  component  (tree)  works  on  its  own  model   •  Peaceful  co-­‐exsitence     MULTIPLE  COMPONENT  HIERARCHIES  (I)   Component 3 Component 2 Component 1 props props callback Model 1 (whatever that means) Component C Component B Component A props props callback Model 2 (whatever that means)

Slide 5

Slide 5 text

Shared  Model  with  independent  component  trees   •  Each  component  (tree)  works  on  the  same  model   •  Can  be  confusing:  who‘s  taking  responsibility?  Can  lead  to  back  loops   MULTIPLE  COMPONENT  HIERARCHIES  (II)   Component 3 Component 2 Component 1 props props callback Component C Component B Component A props props callback Model 1

Slide 6

Slide 6 text

REACT  MEETUP  HAMBURG   FLUX  

Slide 7

Slide 7 text

„ApplicaNon  architecture  for  building  user  interfaces“   •  hGp://facebook.github.io/flux/   •  Design  paGern,  not  a  framework   •  Various  implementa,ons  and  interpreta,ons   •  Not  ,ed  to  React     Introduces  unidirecNonal  data  flow   FLUX  

Slide 8

Slide 8 text

Component  triggers  an  AcNon  using  an  AcNonCreator   UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  1   { UserActionCreators.addUser(...); }} />   Component ĐƟŽŶ trigger

Slide 9

Slide 9 text

AcNonCreator  sends  AcNon  to  Central  Dispatcher     UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  2   addUser(name) { Dispatcher.dispatch({ type: ‘USER_ADD_ACTION‘, payload: { user: name } }); }   (UserActionsCreator) Component ĐƟŽŶ Dispatcher trigger pass into

Slide 10

Slide 10 text

Dispatcher  forwards  the  AcNon  to  Store     UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  3   Component ĐƟŽŶ Dispatcher Store trigger pass into dispatch to

Slide 11

Slide 11 text

Store  processes  AcNon  and  emits  a  change  event     UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  4   handleAction(action){ if (action.type===‘USER_ADD_ACTION‘) { this.users.push(action.payload.user); emitStoreChangeEvent(); } }   (UserStore) Component ĐƟŽŶ Dispatcher Store trigger pass into dispatch to inform

Slide 12

Slide 12 text

Component  receives  event  and  updates  its  state   UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  5   onUserStoreChange() { this.setState( {users: UserStore.getAllUsers();} ); }   Component ĐƟŽŶ Dispatcher Store trigger pass into dispatch to inform

Slide 13

Slide 13 text

Note:  different  implementa4ons  interpret  differently     FLUX  ELEMENTS  IN  DETAILS  

Slide 14

Slide 14 text

„Regular“  React  component   •  Triggers  an  ac,on   •  e.g.  on  user  interac,on     •  Listens  to  one  or  more  Stores   •  Derives  its  state  from  Store   •  Update  children  from  render()  method         Controller  Component   •  Top-­‐level  component  should  be  the   only  component  interac,ng  with  Flux     COMPONENT   ĐƟŽŶ ŽŵƉŽŶĞŶƚ ŽŵƉŽŶĞŶƚ ŽŶƚƌŽůůĞƌŽŵƉŽŶĞŶƚ props state props ^ƚŽƌĞ listen dispatch

Slide 15

Slide 15 text

AcNon   •  Represents  seman&c  event  happend  in   the  app   •  Has  an  iden,fying  „type“     •  Contains  informa,on  what  happend   (payload)   •  Triggered  by  Components       AcNonCreator   •  Factory  for  Ac,ons   •  Ensures  integrity  of  ac,on  object,     •  Seman,c  methods     •  Pass  Ac,on  to  Dispatcher     ACTION  and  ACTIONCREATOR   Component ĐƟŽŶƌĞĂƚŽƌ addUser(...) dispatch ĐƟŽŶ Type WĂLJůŽĂĚ ŝƐƉĂƚĐŚĞƌ

Slide 16

Slide 16 text

•  Pure  technical  component,  no  business  logic   •  Only  one  Dispatcher  in  your  applica,on  (singleton)   •  Forwards  Ac,ons  to  all  registered  Stores   •  Works  synchronous         DISPATCHER  

Slide 17

Slide 17 text

•  Contains  the  business  logic  for  a  specific  domain   •  Conntect  to  the  central  Dispatcher  via  callback  method   •  Process  only  Ac,ons  they  are  interessted  in  (eg  filter  by   type)   •  Emit  events  aZer  changing  model       STORE   Store process Component Component Component ĐƟŽŶ ĐƟŽŶ ĐƟŽŶ event listen update

Slide 18

Slide 18 text

Very  flexible  scenarios…     UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  EXAMPLES   Component ĐƟŽŶ Dispatcher Store trigger pass into dispatch to inform

Slide 19

Slide 19 text

...Ac,on  can  be  dispatched  to  mul,ple  stores   ...A  component  can  listen  to  mul,ple  stores...   UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  EXAMPLE  1   Component ĐƟŽŶ Dispatcher Store Store trigger pass into dispatch to dispatch to inform inform

Slide 20

Slide 20 text

...Mul,pe  components  can  listen  to  same  store,   working  on  same  domain  „model“...   UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  EXAMPLE  2   Component Component ĐƟŽŶ Dispatcher Store trigger pass into dispatch to inform

Slide 21

Slide 21 text

...“external  events“  (e.g.  response  from  server)  can  trigger   ac,ons   •  Flow  is  s,ll  the  same   UNI-­‐DIRECTIONAL  DATA  FLOW  -­‐  EXAMPLE  3   Component Component ĐƟŽŶ ĐƟŽŶ Dispatcher Store trigger trigger pass into dispatch to inform External

Slide 22

Slide 22 text

Useful  when  working  on  same  model     Defined  flow  of  data   •  Easy  to  track   •  Recordable   Lots  of  new  vocabular   •  eg:  what  is  the  shape  of  a  Store?   Many  implemenaNons   •  No  „standard“  implementa,on   SUMMARY  

Slide 23

Slide 23 text

REACT  MEETUP  HAMBURG   THANK  YOU!   Slides:   tbd