dumb and smart components + redux (1)

63
Dumb and smart components & redux Brecht Billiet

Upload: brecht-billiet

Post on 10-Jan-2017

810 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Dumb and smart components + redux (1)

Dumb and smart components & reduxBrecht Billiet

Page 2: Dumb and smart components + redux (1)

About me

Brecht BillietFrontend Architect - Coach - Trainer

http://brecht.io @brechtbilliet

Page 3: Dumb and smart components + redux (1)

Who still uses ng-controller and ng-include?

Page 4: Dumb and smart components + redux (1)

Doesn’t that bother you?

● What HTML belongs to what JS?

● Where do the templates live?

● Absolute template-paths are not maintainable

● What logic belongs in a controller or directive?

● This template should contain... oh wait, there is an ng-include there.

● How do you pass data to your controllers?

Page 5: Dumb and smart components + redux (1)

Think in components...

● 1 piece HTML + 1 piece LOGIC = COMPONENT

● You know it as: element directives

● Custom DOM tags

● A component has a single responsability

● We could use webcomponents + shadow DOM

● Sandboxed work

● Note: angular 1.5 has new component syntax

Page 6: Dumb and smart components + redux (1)

What should you refactor to components?

Page 7: Dumb and smart components + redux (1)
Page 8: Dumb and smart components + redux (1)

Everything is a component

● Your application is a tree of components

● Your pages are components

● Even your application is a component

Page 9: Dumb and smart components + redux (1)

applicationHtml + js+ css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

Page 10: Dumb and smart components + redux (1)

applicationHtml + js+ css

fooPage

html + js + css

html + js + css

barPage

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

html + js + css

Routingconfig:template: ‘<foo-page/>’

Routingconfig:template: ‘<bar-page/>’

Page 11: Dumb and smart components + redux (1)

Easier to debug

Page 12: Dumb and smart components + redux (1)

Other advantages

● No scope soup

● Nice hierarchy view

● Single responsability

● No more absolute paths

● Easier to reason about

● Easier to refactor

● Easier to upgrade to ng2

Page 13: Dumb and smart components + redux (1)

What about attribute-directives?

● Let’s call them behaviors from now

● They add some logic to existing or custom components

● Separate them from your components folder

Page 14: Dumb and smart components + redux (1)

DEMO

Page 15: Dumb and smart components + redux (1)

http://winecellar.surge.sh

Page 16: Dumb and smart components + redux (1)

http://winecellar.surge.sh: components

Page 17: Dumb and smart components + redux (1)

Any rules of thumb?

Page 18: Dumb and smart components + redux (1)
Page 19: Dumb and smart components + redux (1)

Keep them small...

Page 20: Dumb and smart components + redux (1)

Keep them dumb where possible...

Page 21: Dumb and smart components + redux (1)

Dumb component Smart component (container)

Doesn’t know about the application

Knows about the application

Doesn’t do data fetching Does data fetching

Main purpose is visualization Interacts the application

Reusable Not reusable

Only communicates with its direct parent

Passes state and data to its children

Doesn’t do state management Does state management

Page 22: Dumb and smart components + redux (1)

http://winecellar.surge.sh: Smart components

Page 23: Dumb and smart components + redux (1)

http://winecellar.surge.sh: Dumb components

Page 24: Dumb and smart components + redux (1)

State management

● State is very hard to manage in SPA’s

● State can be

○ data: results from REST calls

○ “sidebar is collapsed” or “spinner is active”

○ Which tab is currently active

Page 25: Dumb and smart components + redux (1)

State management

● 3 types of state

○ Data state

○ Application state

○ Inner state (e.g value of textbox) We don’t have to persist nor manage that

Page 26: Dumb and smart components + redux (1)

Why is it so hard to manage?

● 1-way databinding and 2-way databinding

● javascript references that are being broken and sometimes kept

● We can mutate the data in a million different ways

● No structure

● Data flows in different directions

Page 27: Dumb and smart components + redux (1)

application

state & logic

state & logic

state & logic

state & logic

state & logic

state & logic

state & logic

state & logic

state & logic

state & logic

Without flux

Page 28: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

Without flux

Page 29: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

Without flux

Page 30: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

Without flux

Page 31: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

Without flux

Page 32: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

Without flux

Page 33: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

I’m sending an event upwards

Without flux

Page 34: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

I’m sending an event upwards

I’m sending events downwards

Without flux

Page 35: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

I’m sending an event upwards

Without fluxI’m sending events downwards

Page 36: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

I’m sending an event upwards

I get my data from model x and I can update it here I get my data

from model z

Without fluxI’m sending events downwards

I get my data from model y

Page 37: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

I changed

So did I

My changes should reflect 6 and 4

I have to subscribe before 8 sends a message

I’m sending an event upwards

I get my data from model x and I can update it here I get my data

from model z

Without fluxMy data is passed by twoway binding

I’m sending events downwards

I get my data from model y

My data is passed by oneway binding

My data is passed by twoway binding

Page 38: Dumb and smart components + redux (1)

Where do those states/events come from?

Page 39: Dumb and smart components + redux (1)

Who updated my state??

Page 40: Dumb and smart components + redux (1)

Who updated/notified which component?

Page 41: Dumb and smart components + redux (1)
Page 42: Dumb and smart components + redux (1)

Flux helps with that

● Brings structure in data flow

● Not a framework but an architecture by facebook

● Unidirectional dataflow, oneway databinding

● Data flows from the top down

● Easier to reason about state

Page 43: Dumb and smart components + redux (1)

● Action○ Has an action type and a payload

● Dispatcher○ Dispatches the actions to the store

● Store○ Contains state

● ViewComponents

Page 44: Dumb and smart components + redux (1)

● The unidirectional dataflow makes it easy to reason about

Page 45: Dumb and smart components + redux (1)

I use Redux!

Redux is based on flux, but it’s easier

Page 46: Dumb and smart components + redux (1)

Redux

● Written by Dan Abramov (Facebook)

● Easier to use and less bloat

● Only has one store!!

● Completely Immutable data structure

● Uses reducers (pure functions)

● Flux approves

Page 47: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11STORE

Page 48: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

Me too

Me too

Me too

Me too

STORE

I only send actions upwards to the store

I’m the single-source-of-truth in your application!! ;-)

Page 49: Dumb and smart components + redux (1)

1

2

5 6

3

7 8

4

9

10 11

Me too

Me too

I’m dumb I’m dumb

I’m dumb I’m dumb

I’m dumb

I’m dumb

STORE

Me tooI only send actions upwards to the store Me too

I’m the single-source-of-truth in your application!! ;-)

Dumb components only notify their direct parents!

No state management needed here :-)

Page 50: Dumb and smart components + redux (1)

What’s different?

● We have one single source of truth: the store

● We don’t update state ourselves anymore, the store handles all the state of our application

● State is immutable

● Actions are always sent upwards

Page 51: Dumb and smart components + redux (1)

What’s different?

● Dumb components don’t do state management

● State is being updated by pure functions (reducers)

● When the store is updated it notifies all the smart components

Page 52: Dumb and smart components + redux (1)

Application state

Page 53: Dumb and smart components + redux (1)

Actions

● Actions have a type and a payload

● Keep the types together, gives clear overview

Page 54: Dumb and smart components + redux (1)

Actions

Page 55: Dumb and smart components + redux (1)

Reducers

● Pure functions

● reducer(state: MyState, action: Action): MyState

● they update the state

● Completely immutable

Page 56: Dumb and smart components + redux (1)

Reducers

Page 57: Dumb and smart components + redux (1)

Example in container

Page 58: Dumb and smart components + redux (1)

Middleware

● Easy logging, debugging

● Reproducible action state (pushable to server)

● Timetraveling with redux-devtools (requires react to be bundled in the angular project)

Page 59: Dumb and smart components + redux (1)

Check it out

● egghead.io course (Free)

○ https://egghead.io/series/getting-started-with-redux

Page 60: Dumb and smart components + redux (1)

Special thanks to

● Kwinten Pisman (@kwintenp)

● Jurgen Van de Moere (@jvandemo)

Page 61: Dumb and smart components + redux (1)

Questions?

Page 62: Dumb and smart components + redux (1)

One more thing

Angular2, rxjs, redux, @ngrx/store, Typescript, webpackhttp://workshop.brecht.io @brechtbilliet

WorkshopJune 2016

Page 63: Dumb and smart components + redux (1)

Thank you!