F l u x F l u x & & R e a c t R e a c t
We b A p p l i c a t i
- n
D e v e l
- p
me n t Ma r k R e p k a
F l u x & R e a c t F l u x & R e a c t - - PowerPoint PPT Presentation
F l u x & R e a c t F l u x & R e a c t We b A p p l i c a t i o n D e v e l o p me n t Ma r k R e p k a T o p i c s C o v e r e d I n t r o d u c t i o n t o
We b A p p l i c a t i
D e v e l
me n t Ma r k R e p k a
n t r
u c t i
t
b D e v e l
me n t
G e t t
n
s
t h e b a s i c t e r ms a n d t e c h n
i e s f
g e n e r a l w e b s
t w a r e
l u x
A n a r c h i t e c t u r e f
b u i l d i n g c l i e n t
i d e w e b a p p l i c a t i
s
e a c t
A J a v a S c r i p t l i b r a r y f
b u i l d i n g u s e r i n t e r f a c e s
l
i n g n
e s
Ma i n t h i n g s t
e me mb e r
i v e D e mo
u e s t i
s ?
y p e r T e x t Ma r k u p L a n g u a g e ( H T ML )
T h e s t a n d a r d ma r k u p l a n g u a g e u s e d t
r e a t e w e b p a g e s C
s i s t s
t a g s e n c l
e d i n a n g l e b r a c k e t s : < h t ml > s
t h i n g < / h t ml >
a s c a d i n g S t y l e S h e e t s ( C S S )
T h e s t a n d a r d s t y l e s h e e t l a n g u a g e u s e d f
d e s c r i b i n g t h e l
a n d f
ma t t i n g
a d
u me n t w r i t t e n i n a ma r k u p l a n g u a g e , u s u a l l y H T ML . U s e s a J S O N
i k e s t r u c t u r e t
e fj n e c l a s s e s a n d a t t r i b u t e s f
e a c h H T ML t a g
a v a S c r i p t ( J S )
A l s
n
n a s E C MA S c r i p t P r
r a mmi n g l a n g u a g e
H T ML a n d t h e We b E n a b l e s ma n y mo r e d y n a mi c f e a t u r e s
w e b s i t e s a n d w e b a p p l i c a t i
s
O T a c
e l i b r a r y .
T h i s i s j u s t a d e s i g n p a t t e r n
l u x a p p l i c a t i
s h a v e t h r e e ma j
p a r t s
D i s p a t c h e r S t
e s V i e w s ( R e a c t C
e n t s )
t r u c t u r e a n d D a t a F l
U n i d i r e c t i
a l d a t a fm
i s c e n t r a l t
h e F l u x p a t t e r n T h e d i s p a t c h e r , s t
e s a n d v i e w s a r e i n d e p e n d e n t n
e s w i t h d i s t i n c t i n p u t s a n d
t p u t s
e s i g n e d b y F a c e b
a n d I n s t a g r a m
r
i d e d a s O p e n S
r c e s
t w a r e
Data in a Flux application fmows in a single direction: The views may cause a new action response to user input
h e d i s p a t c h e r i s t h e c e n t r a l h u b t h a t ma n a g e s a l l d a t a fm
i n a F l u x a p p l i c a t i
a s n
e a l i n t e l l i g e n c e
i t s
n
I t i s a s i mp l e me c h a n i s m f
d i s t r i b u t i n g t h e a c t i
s t
h e s t
e s
a c h s t
e r e g i s t e r s i t s e l f a n d p r
i d e s a c a l l b a c k
l l s t
e s i n t h e a p p l i c a t i
r e c e i v e t h e a c t i
v i a t h e c a l l b a c k s a n d c a n c h
e t
c t
t h e m F a c e b
, t h e c r e a t
s
F l u x , p r
i d e c
e f
a n e x a mp l e D i s p a t c h e r t
e t s t a r t e d . T h i s i s s e e n i n t h e r e q u i r e ( ‘ fm u x ’ ) . D i s p a t c h e r s t a t e me n t i n t h e c
e t
h e r i g h t .
t
e s c
t a i n t h e a p p l i c a t i
s t a t e a n d l
i c
S i mi l a r t
h e mo d e l a n d c
t r
l e r i n a t r a d i t i
a l MV C a p p l i c a t i
e g i s t e r s i t s e l f w i t h t h e d i s p a t c h e r a n d p r
i d e s i t w i t h a c a l l b a c k
t a i n s a s w i t c h s t a t e me n t t h a t c a n d e c i d e w h a t t
i t h v a r i
s a c t i
s t h a t a r e d i s p a t c h e d b y t h e D i s p a t c h e r
mi t s e v e n t s t
n y l i s t e n i n g V i e w s t e l l i n g t h e m t
p d a t e t h e i r i n t e r n a l s t a t e
l u x w a s d e s i g n e d t
a i r w e l l w i t h t h e R e a c t l i b r a r y w h i c h p r
i d e s t h e U I s i d e
y
r w e b a p p l i c a t i
e n a n a c t i
i s c a p t u r e d b y t h e s t
e a n e v e n t i s e mi t t e d t
n y v i e w t h a t i s l i s t e n i n g t
h a t s t
e .
h e v i e w c a l l s i t s
n s e t S t a t e ( )
f
c e U p d a t e ( ) me t h
s t
p d a t e a c c
d i n g l y
Mo r e
t h i s i n t h e R e a c t s e c t i
n e x t …
u s t t h e U s e r I n t e r f a c e
H a n d l e s t h e V i n t h e MV C d e s i g n p a t t e r n
i mp l e C
e n t s
E a c h r e a c t c
e n t h a n d l e s
e t h i n g
s u a l l y w r i t t e n i n J S X f
ma t
A l l
s f
X ML / H T ML
i k e s y n t a x d i r e c t l y i n t h e J a v a S c r i p t c
e
r a n s l a t e s d i r e c t l y i n t
a v a S c r i p t
C a n b e r u n d i r e c t l y i n a s t a n d a r d w e b b r
s e r w i t h n
d d i t i
a l l i b r a r i e s r e q u i r e d
e s i g n e d b y F a c e b
a n d I n s t a g r a m
P
e r s t h e U I
b
h
t h e s e w e b s i t e s
r
i d e d a s O p e n S
r c e s
t w a r e
S X i s a J a v a S c r i p t s y n t a x e x t e n s i
t h a t l
s s i mi l a r t
ML / H T ML
fg e r s a c
c i s e a n d f a mi l i a r s y n t a x f
d e fj n i n g c
e n t s w i t h
t i
a l a t t r i b u t e s a n d s t a t e
t ' s mo r e f a mi l i a r f
c a s u a l d e v e l
e r s s u c h a s d e s i g n e r s
a c h R e a c t c
e n t i s d i s p l a y e d t h r
g h i t s r e n d e r f u n c t i
. T h i s f u n c t i
r e t u r n s s
J S X t h a t d e fj n e s h
t h e c
e n t w i l l d i s p l a y
t h e p a g e .
h i s R e n d e r f u n c t i
a n d s
h e l p e r J a v a S c r i p t v a r i a b l e s l e t s u s d
r e a l l y u s e f u l t h i n g s !
y n a mi c a l l y c r e a t i n g a n u mb e r
R e a c t c
e n t s f r
v a l u e s i n a J a v a S c r i p t a r r a y
r e c
l e x R e a c t c
e n t s c a n h a v e s
s t a t e i n f
ma t i
t h a t h e l p s t h e m d e c i d e h
t h e y s h
l d r e n d e r a n d d i s p l a y .
T h i s c
l d i n c l u d e d a t a l i k e
r l i t t l e e x a mp l e a r r a y f r
b e f
e
i n i t i a l s t a t e c a n b e d e fj n e d i n a c
e n t b y
e r r i d i n g t h e g e t I n i t i a l S t a t e ( ) f u n c t i
f t h e s t a t e c h a n g e s i t w i l l t r i g g e r a c a l l t
h e r e n d e r me t h
s
h e c
e n t c a n b e u p d a t e d
n t h i s s i mp l e e x a mp l e , s t a t e i s j u s t a s i n g l e i n t e g e r v a l u e c a l l e d ‘ s
N u mb e r ’ w h i c h h
d s t h e n u mb e r
c l i c k s
b B r
s e r V i e w
C
e n t s h a v e t h r e e ma i n p a r t s
t h e i r l i f e c y c l e
u n t i n g : A c
e n t i s b e i n g i n s e r t e d i n t
h e D O M
g e t I n i t i a l S t a t e ( ) i s i n v
e d b e f
e a c
e n t i s mo u n t e d c
e n t Wi l l Mo u n t ( ) i s i n v
e d i mme d i a t e l y b e f
e mo u n t i n g
c u r s c
e n t D i d Mo u n t ( ) i s i n v
e d i mme d i a t e l y a f t e r mo u n t i n g
c u r s
p d a t i n g : A c
e n t i s b e i n g r e
e n d e r e d t
e t e r mi n e i f t h e D O M s h
l d b e u p d a t e d
s h
l d C
e n t U p d a t e ( ) i s i n v
e d w h e n a c
e n t d e c i d e s w h e t h e r a n y c h a n g e s w a r r a n t a n u p d a t e t
h e D O M
n mo u n t i n g : A c
e n t i s b e i n g r e mo v e d f r
t h e D O M
c
e n t Wi l l U n mo u n t ( ) i s i n v
e d i mme d i a t e l y b e f
e a c
e n t i s u n mo u n t e d a n d d e s t r
e d . C l e a n u p s h
l d g
e r e
n e
t h e mo s t i mp
t a n t t h i n g s i n a n y a p p l i c a t i
, w e b
h e r w i s e , i s p e r f
ma n c e . R e a c t h a s s
v e r y i n t e r e s t i n g f e a t u r e s t
e l p i mp r
e i t s p e r f
ma n c e
e r
h e r p
u l a r w e b f r a me w
k s .
h e D
u me n t O b j e c t Mo d e l ( D O M)
S t r u c t u r e d r e p r e s e n t a t i
a d
u me n t
n t h i s c a s e
r w e b p a g e A t r e e s t r u c t u r e
n
e s a n d
j e c t s t h a t e a c h c a n h a v e d i fg e r e n t p r
e r t i e s a n d me t h
s
I n t e r a c t i n g w i t h t h i s D O M
r s e l v e s i s s l
a n d t r i c k y t
a n d l e c
r e c t l y . R e a c t r e mo v e s t h i s d i ffjc u l t y
d e a l i n g d i r e c t l y w i t h t h e w e b p a g e D O M b y i n t r
u c i n g t h e i d e a
a V i r t u a l D O M
Wh e n e v e r y
c a l l s e t S t a t e ( )
a c
e n t , R e a c t w i l l ma r k i t a s d i r t y . A t t h e e n d
t h e e v e n t l
, w h e n a l l
t h e r e n d e r me t h
s h a v e c a s c a d e d t h r
g h , R e a c t l
s a t a l l t h e d i r t y c
e n t s a n d c
a r e s t h e r e s u l t
t h e n e w s t a t e t
h e e x i s t i n g D O M a n d d
s a s i n g l e c a l c u l a t e d u p d a t e .
h t t p : / / c a l e n d a r . p e r f p l a n e t . c
2 1 3 / d i fg / F a c e b
S
t w a r e E n g i n e e r , C h r i s t
h e r C h e d e a u
T h i s me a n s t h a t , p e r a n y n u mb e r
u p d a t e s i n t h a t e v e n t l
, t h e r e i s e x a c t l y
e t i me w h e n t h e D O M i s b e i n g u p d a t e d .
T h i s me t h
i s g r e a t n e w s f
p e r f
ma n c e s i n c e w e a r e u s u a l l y
l y u p d a t i n g n
e s a t t h e b
t
t h e t r e e , n
a t t h e t
! T h i s me a n s t h a t c h a n g e s a r e l
a l i z e d t
h e r e t h e u s e r i n t e r a c t s a n d d
s n
n
ma l l y i n v
v e u p d a t i n g t h e e n t i r e D O M e a c h t i me .
h t t p : / / c a l e n d a r . p e r f p l a n e t . c
2 1 3 / d i fg / F a c e b
S
t w a r e E n g i n e e r , C h r i s t
h e r C h e d e a u
Wh e n b u i l d i n g a n a p p l i c a t i
w i t h R e a c t t h e r e a r e s
e x t r a f e a t u r e s t h a t ma k e d e v e l
me n t e v e n e a s i e r
a l i z a t i
w i t h r e a c t
n t l
O p e n S
r c e l i b r a r y p r
i d e d b y Y a h
s e s t h e s t a n d a r d I C U Me s s a g e s y n t a x P r
i d e s R e a c t C
e n t s f
ma t d a t a a n d s t r i n g s
n i ma t i
s
R e a c t p r
i d e s t h e R e a c t C S S T r a n s i t i
G r
p S u p p
t s b a s i c C S S a n i ma t i
s a n d t r a n s i t i
s Wr a p s a l l
t h e c
e n t s y
a r e i n t e r e s t e d i n a n i ma t i n g S p e c i fj e d i n C S S fj l e b y s
n a me , r e f e r e n c e d i n c
e b y t h a t n a me
h e R e a c t C S S T r a n s i t i
G r
p a d d
a l l
s f
C S S t r a n s i t i
s a n d a n i ma t i
s w h e n a R e a c t c
e n t e n t e r s
l e a v e s t h e D O M
n i ma t i
s a r e t h e n s p e c i fj e d b y n a me i n t h e C S S
e a c t h e l p s y
c r e a t e s ma l l s i mp l e c
e n t s c a n b e e a s i l y r e u s e d t h r
g h
t t h e a p p l i c a t i
e n t s c a n b e c
i n e d t
e t h e r t
r e a t e mo r e c
l e x i n t e r f a c e s
e a c t w i l l a u t
t i c a l l y ma n a g e a l l U I u p d a t e s w h e n y
r u n d e r l y i n g d a t a c h a n g e s
a g e ma r k u p a n d J a v a S c r i p t b e h a v i
a r e d e fj n e d t
e t h e r i n t h e s a me fj l e , t h e J S X f
ma t , ma k i n g c
e e a s y t
e a d a n d f
l
e s i g n e d f
p e r f
ma n c e w i t h l a r g e c
l e x w e b a p p l i c a t i
s
r
s b r
s e r s u p p
t i n F i r e f
, C h r
, I E 8 + , S a f a r i , e t c .
s e d b y i n d u s t r y l e a d e r s s u c h a s F a c e b
, I n s t a g r a m, N e t fm i x , C
e c a d e m y
t t p s : / / g i t h u b . c
r e p k a m0 9 / r e a c t
u x
e s t
C
t a i n s a l l c
e s h
n i n t h i s p r e s e n t a t i
t t p : / / f a c e b
. g i t h u b . i
fm u x /
T h e
i a l d
u me n t a t i
f
F l u x , p r
i d e d b y F a c e b
.
t t p s : / / f a c e b
. g i t h u b . i
r e a c t /
T h e
i a l d
u me n t a t i
f
R e a c t , p r
i d e d b y F a c e b
.