react & redux, how to scale?

66
SON TANG SENIOR ENGINEERING MANAGER KMS React, Redux, how to scale

Upload: kms-technology

Post on 23-Jan-2018

395 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: React & Redux, how to scale?

SON TANG • SENIOR ENGINEERING MANAGER • KMS

React, Redux, how to scale

Page 2: React & Redux, how to scale?

WH Y D O WE U SE R ED U X

WH Y D O WE U SE R EA C T

EL M A R C H I T EC T U R E

Q & A

Agenda

PR O B L EM WI T H SC A L E

Page 3: React & Redux, how to scale?

WHY DO WE USE REACT

Page 4: React & Redux, how to scale?

44

If you want to move a small distance, just walk. If you want to move a great distance you need some help

“”

Page 5: React & Redux, how to scale?

React is a JS library, not a framework

Page 6: React & Redux, how to scale?

React only supports building User

Interfaces

Page 7: React & Redux, how to scale?

We describe User Interfaces with React

and React will take care how to translate those into actual UI

Page 8: React & Redux, how to scale?

UI Definition Browser Render

Page 9: React & Redux, how to scale?

JSX

Page 10: React & Redux, how to scale?

Others put “JS” into your HTML. React

puts “HTML” in your JS

Page 11: React & Redux, how to scale?

JS in HTML JSX

Page 12: React & Redux, how to scale?

Virtual representation

Page 13: React & Redux, how to scale?

React manages and able to optimize how

and when actual UI get sync with virtual representation

Page 14: React & Redux, how to scale?

React will handle the performance

optimization so we could focus to our

own business

Page 15: React & Redux, how to scale?

React force us to write reusable and

composablecomponents

Page 16: React & Redux, how to scale?

WHY DO WE USE REDUX

Page 17: React & Redux, how to scale?

In complicated SPA, our code must

manage more state than ever before

Page 18: React & Redux, how to scale?

We easily lost control over the when, why and how of its state

Page 19: React & Redux, how to scale?

Redux provide a predictable state

container for JS apps by taking away some

powers

Page 20: React & Redux, how to scale?

The state of your whole application in stored in an object tree within a single

store

Page 21: React & Redux, how to scale?

The only way to change the state is to

emit an action

Page 22: React & Redux, how to scale?

Only set of pure functions a.k.a’reducer’ can

manipulate application state

Page 23: React & Redux, how to scale?

Redux just makes managing state of SPA becomes so

much easier

Page 24: React & Redux, how to scale?

But simplicity comes with trade-off

Page 25: React & Redux, how to scale?

PROBLEM WITH SCALE

Page 26: React & Redux, how to scale?
Page 27: React & Redux, how to scale?

The deeper our component tree

becomes, the more knowledge View

Provider has to keep

Page 28: React & Redux, how to scale?
Page 29: React & Redux, how to scale?

And then we allow user to search movie

with MovieSearchBox

Page 30: React & Redux, how to scale?
Page 31: React & Redux, how to scale?

Now we will need a ‘Movie’ view provider

Page 32: React & Redux, how to scale?
Page 33: React & Redux, how to scale?

Since only view provider has access

to Redux world, it has to pass a lot of information to

children component

Page 34: React & Redux, how to scale?

Then, we want to add ‘Clear’ button into

our SearchBox. How difficult this can be ?

Page 35: React & Redux, how to scale?
Page 36: React & Redux, how to scale?

Oh wait, we need to update our

MovieSearchBox as well

Page 37: React & Redux, how to scale?
Page 38: React & Redux, how to scale?

Now we move to Movie view provider

Page 39: React & Redux, how to scale?
Page 40: React & Redux, how to scale?

Our ’Clear’ button now is ready to use.

Simple right ?

Page 41: React & Redux, how to scale?

But our teammate love SearchBox

component so much, they already implemented

BookSearchBox, MediumSearchBox,

etc

Page 42: React & Redux, how to scale?

We have to make changes in ALL

components which are direct or in-direct use

our SearchBox or ‘Clear’ button won’t

work

Page 43: React & Redux, how to scale?

A month later, client wants to have another view

provider using MovieSearchBox

Page 44: React & Redux, how to scale?

Good luck with copying all reducers/ actions creators and remember to update

those in case we have another ‘Clear’

button

Page 45: React & Redux, how to scale?

We cannot scale with that. In order to scale,

we need to resolve problem of lacking

encapsulation

Page 46: React & Redux, how to scale?

Each component should only care

about their business, not children and grand-children

business

Page 47: React & Redux, how to scale?

ELM ARCHITECTURE

Page 48: React & Redux, how to scale?
Page 49: React & Redux, how to scale?

Every component has two parts: view and

updater

Page 50: React & Redux, how to scale?

Elm consider all component as UI

program, which can be extract as stand-

alone module at anytime

Page 51: React & Redux, how to scale?

Elm architecture is a quick win for the

problem of lacking encapsulation we are

facing

Page 52: React & Redux, how to scale?

Let’s re-implement our SearchBox

Page 53: React & Redux, how to scale?
Page 54: React & Redux, how to scale?

Instead of only UI function

(Component), we will have a ‘pure’ update which contain all the default model related

logics

Page 55: React & Redux, how to scale?
Page 56: React & Redux, how to scale?
Page 57: React & Redux, how to scale?

Now we are able to move ‘query’

manipulate logic to out-side of Movie without losing the ability to control it

Page 58: React & Redux, how to scale?

Movie update now only care about its

jobs, not MovieSearchBox or

SearchBox

Page 59: React & Redux, how to scale?

Now we move to ‘hard’ part, adding ‘Clear’

button

Page 60: React & Redux, how to scale?
Page 61: React & Redux, how to scale?

Since we no longer require

MovieSearchBox or Movie to care about SearchBox business,

we don’t have to update things

Page 62: React & Redux, how to scale?

Changing children implementation doesn’t require

changes in its parent or super parent

Page 63: React & Redux, how to scale?

Now you can use SearchBox and

MovieSearchBoxanywhere in your

application without string attached

Page 64: React & Redux, how to scale?

The best part is we still have a global

state capture all of those and parent are fully control over his

children state

Page 65: React & Redux, how to scale?

Let’s solve encapsulation

problem and have fun with adding new

feature

Page 66: React & Redux, how to scale?

Thank you!

SON TANG • SENIOR ENGINEERING MANAGER • KMS