Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

142
Shehet Gregory Grammarly Treasure hunt in the land of Reactive frameworks

Transcript of Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Page 1: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Shehet Gregory Grammarly

Treasure hunt in the land of Reactive frameworks

Page 2: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

• Software Engineer at

• Front-End Digest on

Shehet Gregory

Page 3: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Treasure hunt in the land of Reactive frameworks

Page 4: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

STOP GEEK?

Page 5: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Few animations Blank slides

But I have a STICKERS!!!

Page 6: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Whoami?

Page 7: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Grammarly

Page 8: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Vue.js -> React.js

History 2014-2015

Page 9: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Flux vs ReFlux

History 2014-2015

Page 10: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

EventEmitter?

History

Page 11: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Inspiration

Page 12: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"
Page 13: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Architecture?

Page 14: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MV**?

Page 15: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Anarchy

Page 16: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

War

ReFlux Flux

Page 17: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Dan Abramov @dan_abramov Redux

Page 18: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

JS + ELM = Redux

Page 19: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Immutable data

• State snapshots • Replayable actions • State hydration • Traceability • Time travelling

Page 20: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Silver bullet

Page 21: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Goodbye Flux

Page 22: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Demo. Todo?

Page 23: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux 10 Todo List Demo

Page 24: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

What Inside?

Page 25: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux 100000 Todo List Demo

Page 26: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Results

Add Todo (ms) Toggle Todo (ms)

Redux 2187 887

Page 27: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Stop. Redux? Need to fix our bullet

Page 28: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux Connect?

Page 29: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

React?

Page 30: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Andrew Clark @acdlite

Page 31: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

https://github.com/acdlite/react-fiber-architecture

React Next

Page 32: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Fiber? Not ready.

Page 33: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Try to decouple state

constinitialState={todos:[]}

constinitialState={todos:[],completedNumber:0}

//TodocomponentStatethis.state={completed:false}

Page 34: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Try to decouple state

constinitialState={todos:[]}

constinitialState={todos:[],completedNumber:0}

//TodocomponentStatethis.state={completed:false}

Page 35: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Try to decouple state

constinitialState={todos:[]}

constinitialState={todos:[],completedNumber:0}

//TodocomponentStatethis.state={completed:false}

Page 36: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Not bad

Page 37: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

Page 38: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x2.5 Toggle todo x18

Redux decouple vs Redux

Page 39: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Stop. Is it Redux?

Page 40: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

React 100000 Todo List Demo

Page 41: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

Page 42: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo 15 ms Toggle todo 12 ms

React vs Redux decouple

Page 43: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Michel Weststrate @mweststrate MobX

Page 44: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX 100000 Todo List Demo

Page 45: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

Page 46: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

Page 47: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x2.4 Toggle todo x19

MobX vs Redux

Page 48: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo 60 ms Toggle todo 8 ms

MobX vs React

Page 49: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Magic?

Page 50: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Observable, Mutable data

• Excels at complex, coupled domains • And complex, deep calculations • Mimimal boilerplate • Efficient • Unopinionated • Encourages strong typing

Page 51: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

How it works?

Page 52: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Todo Model and @observable

classTodoModel{@observableid@observabletext@observablecompletedconstructor(id,text){this.id=idthis.text=textthis.completed=false}toggleTodo(){this.completed=!this.completed}}

Page 53: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Todo Model and @observable

classTodoModel{@observableid@observabletext@observablecompletedconstructor(id,text){this.id=idthis.text=textthis.completed=false}toggleTodo(){this.completed=!this.completed}}

Page 54: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Todo Model and @observable

classTodoModel{@observableid@observabletext@observablecompletedconstructor(id,text){this.id=idthis.text=textthis.completed=false}toggleTodo(){this.completed=!this.completed}}

Page 55: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Todo Component with @observer

@observerclassTodoextendsComponent{render(){return(<lionClick={this.props.todo.toggleTodo}style={{textDecoration:this.props.todo.completed?'line-through':'none' }}>{this.props.todo.text}</li>)}}

Page 56: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Todo Component with @observer

@observerclassTodoextendsComponent{render(){return(<lionClick={this.props.todo.toggleTodo}style={{textDecoration:this.props.todo.completed?'line-through':'none'}}>{this.props.todo.text}</li>)}}

Page 57: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

@observable?

Page 58: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

No magic. Only abstraction.

Page 59: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux Predictability through transactional state

MobX Simplicity through minimally defined,

automatically derived state

by @mweststrate

Page 60: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Second popular state management

Page 61: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

30% less code

Redux 208

MobX 157

Page 62: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Stop! Hollywar! Our silver bullet!

redux-actions redux-act

Page 63: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX 100000 with DevTool Todo List Demo

Page 64: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Read morehttps://mobxjs.github.io/mobx/

Page 65: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

npm install mobx mobx-react

Page 66: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

yarn add mobx mobx-react

npm install mobx mobx-react

Page 67: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux & MobX

Page 68: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Immutable & Mutable Data

Page 69: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX-State-Tree

Page 70: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Benefit•snapshot-able •state container •replayable actions •efficient •transparent reactive derivations •patches •middleware •references •dependency injection

Page 71: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX-State-Tree-Redux 100000 Todo List Demo

Page 72: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Stack• redux actions • redux dispatching • redux provider & connect • redux devtools • redux store • redux reducers • mobx-state-tree factories • mobx-state-tree actions

Page 73: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Redux? MobX? How it works?

Page 74: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

createFactoryconstTodoStore=createFactory({todos:arrayOf(Todo),findTodoById:function(id){returnthis.todos.find(todo=>todo.id===id) },[ADD_TODO]:action(function({text}){this.todos.unshift({id:nextTodoId++,text})}),[TOGGLE_TODO]:action(function({id}){consttodo=this.findTodoById(id)todo.completed=!todo.completed})})

Page 75: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

createFactoryconstTodoStore=createFactory({todos:arrayOf(Todo),findTodoById:function(id){returnthis.todos.find(todo=>todo.id===id)},[ADD_TODO]:action(function({text}){this.todos.unshift({id:nextTodoId++,text})}),[TOGGLE_TODO]:action(function({id}){consttodo=this.findTodoById(id)todo.completed=!todo.completed})})

Page 76: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Mock Storeconsttodos=window.todos=todosFactory(initialState)

conststore=asReduxStore(todos)

connectReduxDevtools(todos)

Page 77: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Mock Storeconsttodos=window.todos=todosFactory(initialState)

conststore=asReduxStore(todos)

connectReduxDevtools(todos)

Page 78: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Mock Storeconsttodos=window.todos=todosFactory(initialState)

conststore=asReduxStore(todos)

connectReduxDevtools(todos)

Page 79: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

Page 80: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

Page 81: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x1.75 Toggle todo 52 ms

MobX State Tree Redux vs Redux

Page 82: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Read morehttps://github.com/mobxjs/mobx-state-tree

Page 83: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

yarn add mobx-state-tree

Page 84: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

What about FRP?

Page 85: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Vesa Karvonen @VesaKarvonen Calmm-js

Page 86: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Bacon or Kefir?

Page 87: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

BRAL BACON REACTIVE ATOM LENS

Page 88: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

KRAL KEFIR REACTIVE ATOM LENS

Page 89: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Bral 10000 Todo List Demo

Page 90: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Kral 10000 Todo List Demo

Page 91: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Slowwwwly…..

Page 92: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Problemfind(t=>t.id===id)

Page 93: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

List to Map

Page 94: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Kefir is faster

Page 95: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Kral 100000 Todo List Demo

Page 96: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

KRAL 1467 253

Page 97: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

KRAL 1467 253

Page 98: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x2.4 Toggle todo x3.5

KRAL vs Redux

Page 99: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x1.6 (562 ms) Toggle todo x5.6 (208 ms)

KRAL vs MobX

Page 100: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Atomconsttodo=Atom({text:'Sometext',completed:false})todo.get()//{text:"Sometext",completed:false}todo.view('text').get()//'Sometext'todo.view('text').set('Anothertext')//'Anothertext'todo.view('completed').modify(completed=>!completed) //false

Page 101: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Atom .get()/set()consttodo=Atom({text:'Sometext',completed:false})todo.get()//{text:"Sometext",completed:false}todo.view('text').get()//'Sometext'todo.view('text').set('Anothertext')//'Anothertext'todo.view('completed').modify(completed=>!completed) //false

Page 102: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Lens/Viewconsttodo=Atom({text:'Sometext',completed:false})todo.get()//{text:"Sometext",completed:false}todo.view('text').get()//'Sometext'todo.view('text').set('Anothertext')//'Anothertext'todo.view('completed').modify(completed=>!completed) //false

Page 103: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Atom .modify()consttodo=Atom({text:'Sometext',completed:false})todo.get()//{text:"Sometext",completed:false}todo.view('text').get()//'Sometext'todo.view('text').set('Anothertext')//'Anothertext'todo.view('completed').modify(completed=>!completed)//false

Page 104: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Reactive HTMLconstTodo=({todo})=><K.lionClick={()=>todo.view('completed').modify(completed=>!completed)}style={{textDecoration:todo.view('completed').map(completed=>completed?'line-through':'none') }}>{todo.view('text')}</K.li>

Page 105: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Reactive HTMLconstTodo=({todo})=><K.lionClick={()=>todo.view('completed').modify(completed=>!completed)}style={{textDecoration:todo.view('completed').map(completed=>completed?'line-through':'none') }}>{todo.view('text')}</K.li>

Page 106: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Reactive HTMLconstTodo=({todo})=><K.lionClick={()=>todo.view('completed').modify(completed=>!completed)}style={{textDecoration:todo.view('completed').map(completed=>completed?'line-through':'none')}}>{todo.view('text')}</K.li>

Page 107: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Reactive HTMLconstTodo=({todo})=><K.lionClick={()=>todo.view('completed').modify(completed=>!completed)}style={{textDecoration:todo.view('completed').map(completed=>completed?'line-through':'none') }}>{todo.view('text')}</K.li>

Page 108: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Kral(Kefir) K* Bral (Bacon) B*

Page 109: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Welcome Karet

importReactfrom'karet'

Page 110: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Karet TodoconstTodo=({todo})=><lionClick={()=>todo.view('completed').modify(completed=>!completed)}style={{textDecoration:todo.view('completed').map(completed=>completed?'line-through':'none'),cursor:'pointer'}}>{todo.view('text')}</li>

Page 111: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Karet 100000 Todo List Demo

Page 112: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

KRAL 1467 253

Karet 1140 167

Page 113: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

Redux 2187 887

Redux decouple state 860 49

React 845 37

MobX 905 45

MobX State Tree Redux 3813 835

KRAL 1467 253

Karet 1140 167

Page 114: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x1.9 (1024 ms) Toggle todo x5.3

Karet vs Redux

Page 115: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x1.25 (235 ms) Toggle todo x3.7 (132 ms)

KRAL vs MobX

Page 116: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Add todo x1.28 (327 ms) Toggle todo x3.7 (122 ms)

Karet vs KRAL

Page 117: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Benefits• Immutable data • Observables • Optimized HTML rendering

Page 118: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

yarn add kral-* karate

Page 119: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Read morehttps://github.com/calmm-js

Page 120: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

React 845 37

Redux decouple state 860 49

MobX 905 45

Karet 1140 167

KRAL 1467 253

Redux 2187 887

MobX State Tree Redux 3813 835

Page 121: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

React 845 37

MobX 905 45

Karet 1140 167

KRAL 1467 253

Redux 2187 887

Page 122: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

ResultsAdd Todo (ms) Toggle Todo (ms)

MobX 905 45

Karet (KRAL) 1140 167

Redux 2187 887

Page 123: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Back to React

Page 124: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

JS is Overhype

Page 125: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

What is good or bad?

Page 126: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

React + View = ❤

Page 127: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Architecture?

Page 128: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Pure Functions

Page 129: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Actions

Page 130: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

State management?

Page 131: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Mutabel vs Immutable

Page 132: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Observer

Page 133: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

View Update

Page 134: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Observable Components

Page 135: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX @4.0

<Observer>{()=><div>{this.props.observer}</div>}</Observer>

Page 136: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Inspiration

Page 137: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Try to code your architecture

Page 138: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

MobX–Reactor

https://github.com/amsb/mobx-reactor

MobX inspired by redux, react-redux and redux-saga

Page 139: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

What next?React.js Conf 2016 - Andrew Clark - Back to React

https://www.youtube.com/watch?v=ZVYVtUFDf28

Page 140: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

What next?You Might Not Need Redux - Dan Abramov

https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367

Page 141: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Think about your product and architecture

Page 142: Григорий Шехет "Treasure hunt in the land of Reactive frameworks"

Thank you! Questions are welcome

@AGambit95