Introduce flux & react in practice

31
Introduce Flux & React in practice ReactJS.tw - meetup 2 @randylien

description

A brief introduction about Flux & React.

Transcript of Introduce flux & react in practice

Page 1: Introduce flux & react in practice

IntroduceFlux & React

in practice

ReactJS.tw - meetup 2

@randylien

Page 2: Introduce flux & react in practice

About Me• Randy Lien • HTML, CSS, JavaScript, Python • Backbone/Marionette/React • PC-Cillin, SafeSync • KKBOX • @randylien

Page 3: Introduce flux & react in practice

Preface

• Everything you see today is under construction.

• This is my personal experience sharing.

• If there are anything I mentioned is not correct, please feel free to point out.

Page 4: Introduce flux & react in practice

Flux & React

• Flux is an architecture

• Dispatcher

• There are many extensions* for Flux

• One direction data flow

• React is a library to create View

• Component

• JSX

• Compiled

Page 5: Introduce flux & react in practice

Flux & React

• React is a library to create View

• Component

• JSX

• Compiled

• Flux is an architecture

• Dispatcher

• There are many extensions* for Flux

• One direction data flow

Data Presentation

Page 6: Introduce flux & react in practice

What Is Flux

Page 7: Introduce flux & react in practice

– Dictionary, in your Mac

“The action or process of flowing or flowing out.”

Page 8: Introduce flux & react in practice

Dr.Brown explains• Flux Capacitor

• Flux = Flow of time.

• Capacitor = Temporary Storage.

• Flux Capacitor must be powered by a small Reactor.

• Time space continuum is circular in nature.

http://backtothefuture.wikia.com/wiki/File:Flux-capacitor-back-to-the-future.jpgAsk Facecbook’s react/flux team, why call flux & react ?Dose it come from Back to The Future ?

Page 9: Introduce flux & react in practice

Flux & React

• Flux just like time, it is one way direction to flow

• Capacitor is Store

• Event trigger from React

• The flow is circular in nature

http://www.8ball.co.uk/media/catalog/product/b/a/back_to_the_future_-_flux_capacitor_-_yel_mens_4_1.jpg

Page 10: Introduce flux & react in practice

Flux is circular in nature

http://facebook.github.io/react/img/blog/flux-diagram.png

Page 11: Introduce flux & react in practice

Store• Kind of model

• Callbacks will trigger Store’s internal methods

• Update data

• Trigger Store’s change event

• Store will return data to React

• React’s Virtual DOM will handle your data

Page 12: Introduce flux & react in practice

Events from React

https://www.facebook.com/photo.php?fbid=10154774642145430&set=gm.1491610957781164&type=1&theater

Page 13: Introduce flux & react in practice

Important

http://facebook.github.io/react/img/blog/flux-diagram.png

Page 14: Introduce flux & react in practice

Store & React

Page 15: Introduce flux & react in practice

store

store store

store

app.js

App get data from Store

Page 16: Introduce flux & react in practice

app.js

store store storestore

Set Property to your Component

Page 17: Introduce flux & react in practice

component

store store storestore

component

component

component

Set Properties to your sub Components

Page 18: Introduce flux & react in practice

componentstore

component

component

componentstore

store

store

Sub Components get properties

Page 19: Introduce flux & react in practice

component

component

component

component

component

component

component

component

component

component

component

component

component

Continue to dispatch properties to sub components

Page 20: Introduce flux & react in practice

component

component

component

component

component

component

component

component

component

component

component

component

component

React’s Virtual DOM will handle the value for you

Page 21: Introduce flux & react in practice

Important

http://facebook.github.io/react/img/blog/flux-diagram.png

Page 22: Introduce flux & react in practice

Practices for Flux• Use console.log in Store

• Data should not be changed in other place

• Use shouldComponentUpdate wisely

• Render correctly

• Improve performance

Page 23: Introduce flux & react in practice

Use Case 1. Music Player

• Development time: ~4 weeks

• Pure Flux architecture

• 4 Stores (PlayerStore, ChannelStore,

UserStore, AppStore) 30 Components, 40 Constants

Page 24: Introduce flux & react in practice

DEMO

Page 25: Introduce flux & react in practice

Use Case 2. Mobile Store

• Development time: ~3 weeks

• Use Fluxxor

• Director as Router

• 9 Stores, 13 Components, 16 Layouts

Page 26: Introduce flux & react in practice

DEMO

Page 27: Introduce flux & react in practice

Flux implementation• http://fluxxor.com/

• https://github.com/spoike/refluxjs

• https://github.com/jmreidy/fluxy

• https://github.com/yahoo/dispatchr

• https://github.com/yahoo/fluxible-app

• https://github.com/kenwheeler/mcfly

• http://deloreanjs.com/

• http://www.jflux.io

Page 28: Introduce flux & react in practice

- Randy

“Understand the architecture before you leverage any implementations.”

Page 29: Introduce flux & react in practice

By the way

Page 30: Introduce flux & react in practice

f.lux

Page 31: Introduce flux & react in practice

Q&A