Slide 1

Slide 1 text

THE State of the state: React State Management IN 2019 @YTHECOMBINATOR

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

1PUB/42m

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Bring me that!

Slide 10

Slide 10 text

"""

Slide 11

Slide 11 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal REDUX-ACT IMMER

Slide 12

Slide 12 text

https://github.com/GantMan/ReactStateMuseum

Slide 13

Slide 13 text

"Javascript Fatigue" by Eric Cle!"ons

Slide 14

Slide 14 text

HAS THE SO-CALLED JAVASCRIPT FATIGUE REACHED (REACT) STATE MANAGEMENT?

Slide 15

Slide 15 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 16

Slide 16 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 17

Slide 17 text

Tldr; Offload store management logic to a separate web worker.

Slide 18

Slide 18 text

https://github.com/developit/stockroom

Slide 19

Slide 19 text

DEMO

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

HIGHLIGHTS: ↝ + Performance ↝ Good for CPU-bound store mutations

Slide 24

Slide 24 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 25

Slide 25 text

Tldr; Declaratively describing the behaviour of your application

Slide 26

Slide 26 text

FEATURING: ↝ A list of states ↝ One initial state ↝ A list of events that trigger transitions

Slide 27

Slide 27 text

DEMO

Slide 28

Slide 28 text

HIGHLIGHTS: ↝ Preciseness of specs ↝ Correctness of code ↝ Less unimportant state / Less if clauses ↝ Leverage knowledge from non-coders too

Slide 29

Slide 29 text

CONS: ↝ Surely an overkill for super simple components ↝ Lack of familiarity from developers and designers ↝ Small ecosystem

Slide 30

Slide 30 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 31

Slide 31 text

Tldr; Combine your local and remote state management

Slide 32

Slide 32 text

Key Concepts: ↝ DEFAULTS: Your base state. What you start with. ↝ RESOLVERS: Where all the magic happens to retrieve and update your local data in the Apollo cache.

Slide 33

Slide 33 text

DEMO

Slide 34

Slide 34 text

HIGHLIGHTS: ↝ Getting Apollo set up just for state management can be a bit of work ↝ A natural way of querying and mutating state

Slide 35

Slide 35 text

cons: ↝ Surely an overkill for apps that don't heavily depend on remote state

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 38

Slide 38 text

Tldr; Redux best practices without the boilerplate.

Slide 39

Slide 39 text

https://github.com/jamiebuilds/unstated

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

https://github.com/pedronauck/reworm #

Slide 42

Slide 42 text

import React from 'react' import { Provider, create } from 'reworm' const { set, get } = create({ name: 'John' }) class App extends React.Component { componentDidMount() { set(prev $% ({ name: 'Peter' + prev.name })) } render() { return (
{get(s $% s.name)}
) } }

Slide 43

Slide 43 text

HIGHLIGHTS: ↝ - Boilerplate ↝ + Simplicity

Slide 44

Slide 44 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 45

Slide 45 text

Tldr; Recreate Redux in our React app without actually installing redux and react-redux

Slide 46

Slide 46 text

Key Concepts: ↝ CONTEXT: Provides a way to pass data through the component tree without having to pass props down manually at every level. ↝ HOOKS: Let you use state and other React features without writing a class

Slide 47

Slide 47 text

DEMO

Slide 48

Slide 48 text

HIGHLIGHTS: ↝ Less boilerplate than redux itself ↝ No third-party dependencies

Slide 49

Slide 49 text

cons: ↝ Still carries some of redux boilerplate ↝ You might face a few perf issues

Slide 50

Slide 50 text

Split contexts that don't change together function Button() { let theme = useContext(ThemeContext); &' The rest of your rendering logic return ; }

Slide 51

Slide 51 text

Split your component in two, put memo in between function Button() { let appContextValue = useContext(AppContext); let theme = appContextValue.theme; &' Your "selector" return } const ThemedButton = memo(({ theme }) $% { &' The rest of your rendering logic return ; });

Slide 52

Slide 52 text

One component with useMemo inside function Button() { let appContextValue = useContext(AppContext); let theme = appContextValue.theme; &' Your "selector" return useMemo(() $% { &' The rest of your rendering logic return ; }, [theme]) }

Slide 53

Slide 53 text

HAS THE SO-CALLED JAVASCRIPT FATIGUE REACHED (REACT) STATE MANAGEMENT?

Slide 54

Slide 54 text

HAS THE SO-CALLED JAVASCRIPT FATIGUE REACHED (REACT) STATE MANAGEMENT? Maybe! BUT SO HAS THE VANILLA WAY OF DOING THINGS!

Slide 55

Slide 55 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 56

Slide 56 text

https://github.com/pauldijou/redux-act

Slide 57

Slide 57 text

const serializeTodo = createAction('SERIALIZE_TODO'); serializeTodo(1); &' return { type: 'SERIALIZE_TODO', payload: 1 }

Slide 58

Slide 58 text

const loading = createReducer({}, initialState.loading) .on(actions.fetch, () $% true) .on(actions.setQuery, () $% true) const ids = createReducer({}, initialState.ids) .on(actions.setIds, (state, payload) $% [&&.payload]) .on(actions.delete, (state, payload) $% { return [&&.state.filter((id) $% id ))* payload)]; });

Slide 59

Slide 59 text

HIGHLIGHTS: ↝ Less boilerplate than plain redux ↝ Piping syntax for reducers

Slide 60

Slide 60 text

Apollo link state Unstated Stockroom alfa MOBX setState() Redux CONTEXT HOOKS reworm Relay React Automata freactal IMMER REDUX-ACT

Slide 61

Slide 61 text

⚠ INTERMISSION ⚠ Immutability

Slide 62

Slide 62 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant

Slide 63

Slide 63 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant

Slide 64

Slide 64 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant

Slide 65

Slide 65 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant

Slide 66

Slide 66 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant Full Reconciliation REACT Reconciliation REACT + IMMUTABLE

Slide 67

Slide 67 text

WHY: ↝ - Breakable ↝ + Debuggable ↝ + Performant Full Reconciliation O(N^3) REACT Reconciliation O(N) REACT + IMMUTABLE O(LOGN)

Slide 68

Slide 68 text

https://github.com/immutable-js/immutable-js

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

~300

Slide 71

Slide 71 text

https://github.com/kolodny/immutability-helper

Slide 72

Slide 72 text

import update from "i!"utability-helper" const reducer = (state, action) $% { switch (action.type) { case ADD_TAG: return update(state, { [action.id]: { tags: { $push: [action.tag] } } }) default: return state } }

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

https://github.com/immerjs/immer

Slide 75

Slide 75 text

const map1 = { foo: 1, bar: 2 } const map2 = I!"utable.Map(map1) const { foo, bar } = map2 console.log(foo) &' undefined console.log(bar) &' undefined

Slide 76

Slide 76 text

const map1 = { foo: 1, bar: 2 } const map2 = produce(map1, draft $% { draft.foo += 10 }) const { foo, bar } = map2 console.log(foo) &' 11 console.log(map1.bar ++, bar) &' true

Slide 77

Slide 77 text

exposes a single default function that does all the work const map1 = { foo: 1, bar: 2 } const map2 = produce(map1, draft $% { draft.foo += 10 }) const { foo, bar } = map2 console.log(foo) &' 11 console.log(map1.bar ++, bar) &' true

Slide 78

Slide 78 text

const map1 = { foo: 1, bar: 2 } const map2 = produce(map1, draft $% { draft.foo += 10 }) const { foo, bar } = map2 console.log(foo) &' 11 console.log(map1.bar ++, bar) &' true apply all your changes to a temporarily draft State, which is a proxy of the currentState.

Slide 79

Slide 79 text

your objects and arrays are really JavaScript objects and arrays, so you can do everything you would normally do const map1 = { foo: 1, bar: 2 } const map2 = produce(map1, draft $% { draft.foo += 10 }) const { foo, bar } = map2 console.log(foo) &' 11 console.log(map1.bar ++, bar) &' true

Slide 80

Slide 80 text

&' add a tag to a todo { &&.state, [action.id]: { &&.state[action.id], tags: [ &&.state[action.id].tags, action.tag ] } } &' add a tag to a todo state[action.id].tags.push(action.tag)

Slide 81

Slide 81 text

ALSO: ↝ ES5 Fallback ↝ Strongly typed ↝ Small (4KB) ↝ Currying

Slide 82

Slide 82 text

ALSO: ↝ Fast Auto freezing ↝ Equivalent perf to ImmutableJS ↝ Supports hooks!

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

⚠ ❌ ❌ ⚠

Slide 86

Slide 86 text

~ senior software engineer, Front-End @beakyn ~ @ythecombinator on the webs ~ addicted to emojis, memes and beer

Slide 87

Slide 87 text

https://bit.ly/react-state-management-in-2019

Slide 88

Slide 88 text

THANKS! @YTHECOMBINATOR