Everything I Know About React I Learned From Twitter

A presentation at Reactathon in March 2019 in San Francisco, CA, USA by Jenn Creighton

Slide 1

Slide 1

Everything I know about React I learned from Twitter Jenn Creighton (@gurlcode) FrontEnd Engineer @ Rent the RunwaY

Slide 2

Slide 2

React Detective ! @gurlcode

Slide 3

Slide 3

On a dark & stormy night… @gurlcode

Slide 4

Slide 4

@gurlcode

Slide 5

Slide 5

@gurlcode

Slide 6

Slide 6

🔎 @gurlcode

Slide 7

Slide 7

Slide 8

Slide 8

@gurlcode

Slide 9

Slide 9

Welcome! @gurlcode

Slide 10

Slide 10

@gurlcode

Slide 11

Slide 11

@gurlcode

Slide 12

Slide 12

@gurlcode

Slide 13

Slide 13

@gurlcode

Slide 14

Slide 14

<button /> @gurlcode

Slide 15

Slide 15

React.createElement(”button”, null); @gurlcode

Slide 16

Slide 16

React Element { type: button, props: { … } } @gurlcode

Slide 17

Slide 17

@gurlcode

Slide 18

Slide 18

@gurlcode

Slide 19

Slide 19

@gurlcode

Slide 20

Slide 20

@gurlcode

Slide 21

Slide 21

@gurlcode

Slide 22

Slide 22

@gurlcode

Slide 23

Slide 23

@gurlcode

Slide 24

Slide 24

Fiber { tag child sibling return … } @gurlcode

Slide 25

Slide 25

Fiber { Fiber type tag child sibling return … } @gurlcode

Slide 26

Slide 26

Fiber { Child fiber Fiber type tag child sibling return … } @gurlcode

Slide 27

Slide 27

Fiber { Child fiber Fiber type tag child sibling return … Sibling fiber } @gurlcode

Slide 28

Slide 28

Fiber { Child fiber Parent fiber Fiber type tag child sibling return … Sibling fiber } @gurlcode

Slide 29

Slide 29

@gurlcode

Slide 30

Slide 30

reactInternalInstance @gurlcode

Slide 31

Slide 31

@gurlcode

Slide 32

Slide 32

@gurlcode

Slide 33

Slide 33

@gurlcode

Slide 34

Slide 34

@gurlcode

Slide 35

Slide 35

@gurlcode

Slide 36

Slide 36

div h1 C div one-way two-way @gurlcode

Slide 37

Slide 37

@gurlcode

Slide 38

Slide 38

ReactDOM.unstable_createRoot(node) @gurlcode

Slide 39

Slide 39

new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 40

Slide 40

createFiberRoot new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 41

Slide 41

createHostRootFiber createFiberRoot new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 42

Slide 42

HostRoot createFiber fiber { tag: HostRoot } createFiberRoot new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 43

Slide 43

HostRoot createFiber root.current = fiber new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 44

Slide 44

HostRoot createFiber new ReactRoot ReactDOM.unstable_createRoot(node) @gurlcode

Slide 45

Slide 45

HostRoot createFiber this._internalRoot = root ReactDOM.unstable_createRoot(node) @gurlcode

Slide 46

Slide 46

HostRoot createFiber root = ReactRoot @gurlcode

Slide 47

Slide 47

@gurlcode

Slide 48

Slide 48

this._InternalRoot { current: { tag: HostRoot } … } @gurlcode

Slide 49

Slide 49

HostRoot root.render( ReactElement ) @gurlcode

Slide 50

Slide 50

HostRoot ReactRoot.render( ReactElement ) @gurlcode

Slide 51

Slide 51

HostRoot scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 52

Slide 52

HostRoot update = { element: element } ReactRoot.render( ReactElement ) @gurlcode

Slide 53

Slide 53

HostRoot enqueueUpdate(update) scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 54

Slide 54

Fiber { alternate tag child sibling list of state updates return updateQueue } @gurlcode

Slide 55

Slide 55

HostRoot scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 56

Slide 56

HostRoot scheduleWork( ) scheduleWork scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 57

Slide 57

HostRoot scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 58

Slide 58

HostRoot scheduleRootUpdate(current, element) ReactRoot.render( ReactElement ) @gurlcode

Slide 59

Slide 59

@gurlcode

Slide 60

Slide 60

HostRoot performWork @gurlcode

Slide 61

Slide 61

HostRoot renderRoot performWork @gurlcode

Slide 62

Slide 62

HostRoot createWorkInProgress(current) renderRoot performWork @gurlcode

Slide 63

Slide 63

HostRoot workInProgress = current.alternate renderRoot performWork @gurlcode

Slide 64

Slide 64

Fiber { Pooled fiber alternate tag child sibling return … } @gurlcode

Slide 65

Slide 65

@gurlcode

Slide 66

Slide 66

@gurlcode

Slide 67

Slide 67

HostRoot workInProgress === null renderRoot performWork @gurlcode

Slide 68

Slide 68

HostRoot createFiber(current) workInProgress = current.alternate renderRoot performWork @gurlcode

Slide 69

Slide 69

HostRoot current.alternate = workInProgress renderRoot performWork @gurlcode

Slide 70

Slide 70

HostRoot createWorkInProgress(current) renderRoot performWork @gurlcode

Slide 71

Slide 71

HostRoot nextUnitOfWork = workInProgress performWork @gurlcode

Slide 72

Slide 72

HostRoot workLoop nextUnitOfWork = workInProgress performWork @gurlcode

Slide 73

Slide 73

@gurlcode

Slide 74

Slide 74

fiber @gurlcode

Slide 75

Slide 75

HostRoot workLoop @gurlcode

Slide 76

Slide 76

HostRoot performUnitOfWork workLoop @gurlcode

Slide 77

Slide 77

HostRoot beginWork performUnitOfWork workLoop @gurlcode

Slide 78

Slide 78

HostRoot updateHostRoot beginWork performUnitOfWork workLoop @gurlcode

Slide 79

Slide 79

HostRoot processUpdateQueue updateHostRoot beginWork performUnitOfWork workLoop @gurlcode

Slide 80

Slide 80

HostRoot newState = { element: element } updateHostRoot beginWork performUnitOfWork workLoop @gurlcode

Slide 81

Slide 81

HostRoot fiber.memoizedState = newState updateHostRoot beginWork performUnitOfWork workLoop @gurlcode

Slide 82

Slide 82

Fiber { alternate tag child sibling return memoizedState … current state } @gurlcode

Slide 83

Slide 83

HostRoot nextChild = fiber.memoizedState.element beginWork performUnitOfWork workLoop @gurlcode

Slide 84

Slide 84

HostRoot reconcileChildFibers(child, nextChild) nextChild = fiber.memoizedState.element beginWork performUnitOfWork workLoop @gurlcode

Slide 85

Slide 85

HostRoot workInProgress.child = createFiber( ) div nextChild = fiber.memoizedState.element beginWork performUnitOfWork workLoop @gurlcode

Slide 86

Slide 86

HostRoot div return workInProgress.child beginWork performUnitOfWork workLoop @gurlcode

Slide 87

Slide 87

HostRoot div return workInProgress.child performUnitOfWork workLoop @gurlcode

Slide 88

Slide 88

HostRoot div return workInProgress.child workLoop @gurlcode

Slide 89

Slide 89

HostRoot div nextUnitOfWork = performUnitWork @gurlcode

Slide 90

Slide 90

HostRoot div nextUnitOfWork !== null @gurlcode

Slide 91

Slide 91

HostRoot div workLoop @gurlcode

Slide 92

Slide 92

HostRoot div performUnitOfWork workLoop @gurlcode

Slide 93

Slide 93

HostRoot div beginWork performUnitOfWork workLoop @gurlcode

Slide 94

Slide 94

HostRoot div updateHostComponent beginWork performUnitOfWork workLoop @gurlcode

Slide 95

Slide 95

HostRoot div nextChild = fiber.pendingProps.children beginWork performUnitOfWork workLoop @gurlcode

Slide 96

Slide 96

Fiber { alternate tag child sibling return memoizedState pendingProps … waiting to be applied } @gurlcode

Slide 97

Slide 97

HostRoot reconcileChildFibers(child, nextChild) div updateHostComponent beginWork performUnitOfWork workLoop @gurlcode

Slide 98

Slide 98

reconcileChildrenArray reconcileChildFibers HostRoot div updateHostComponent beginWork performUnitOfWork workLoop @gurlcode

Slide 99

Slide 99

HostRoot child = createFiber( ) reconcileChildFibers div updateHostComponent h1 beginWork performUnitOfWork workLoop @gurlcode

Slide 100

Slide 100

HostRoot child.sibling = createFiber( ) reconcileChildFibers div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 101

Slide 101

HostRoot return child reconcileChildFibers div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 102

Slide 102

HostRoot workInProgress.child = child div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 103

Slide 103

HostRoot div return workInProgress.child h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 104

Slide 104

HostRoot div h1 C return workInProgress.child performUnitOfWork workLoop @gurlcode

Slide 105

Slide 105

HostRoot div h1 C return workInProgress.child workLoop @gurlcode

Slide 106

Slide 106

HostRoot div h1 C nextUnitOfWork = performUnitWork @gurlcode

Slide 107

Slide 107

HostRoot div h1 C nextUnitOfWork !== null @gurlcode

Slide 108

Slide 108

HostRoot div h1 C workLoop @gurlcode

Slide 109

Slide 109

HostRoot div h1 C performUnitOfWork workLoop @gurlcode

Slide 110

Slide 110

HostRoot div h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 111

Slide 111

HostRoot div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 112

Slide 112

HostRoot reconcileChildFibers div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 113

Slide 113

HostRoot workInProgress.child = null div updateHostComponent h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 114

Slide 114

HostRoot div return null h1 C beginWork performUnitOfWork workLoop @gurlcode

Slide 115

Slide 115

HostRoot div h1 C return null performUnitOfWork workLoop @gurlcode

Slide 116

Slide 116

HostRoot div h1 C nextUnitOfWork === null workLoop @gurlcode

Slide 117

Slide 117

HostRoot div h1 C completeUnitOfWork performUnitOfWork workLoop @gurlcode

Slide 118

Slide 118

HostRoot div completeWork h1 C completeUnitOfWork return performUnitOfWork workInProgress.child workLoop @gurlcode

Slide 119

Slide 119

HostRoot div return fiber.sibling | | fiber.return h1 C completeUnitOfWork return performUnitOfWork workInProgress.child workLoop @gurlcode

Slide 120

Slide 120

HostRoot div h1 C return fiber.sibling return performUnitOfWork workInProgress.child workLoop @gurlcode

Slide 121

Slide 121

HostRoot div h1 C return fiber.sibling return return workInProgress.child fiber.sibling workLoop @gurlcode

Slide 122

Slide 122

HostRoot div h1 C return fiber.sibling nextUnitOfWork = performUnitOfWork return workInProgress.child nextUnitOfWork = performUnitOfWork @gurlcode

Slide 123

Slide 123

HostRoot div h1 C @gurlcode

Slide 124

Slide 124

HostRoot div h1 C div @gurlcode

Slide 125

Slide 125

HostRoot div h1 C div @gurlcode

Slide 126

Slide 126

HostRoot div h1 C div @gurlcode

Slide 127

Slide 127

HostRoot div h1 C div @gurlcode

Slide 128

Slide 128

HostRoot div h1 C div @gurlcode

Slide 129

Slide 129

@gurlcode

Slide 130

Slide 130

Slide 131

Slide 131

@gurlcode

Slide 132

Slide 132

@gurlcode

Slide 133

Slide 133

@gurlcode

Slide 134

Slide 134

@gurlcode

Slide 135

Slide 135

Component.prototype.setState @gurlcode

Slide 136

Slide 136

HostRoot div h1 C div enqueueSetState(fiber, update) @gurlcode

Slide 137

Slide 137

HostRoot div h1 enqueueUpdate(fiber, update) C div enqueueSetState(fiber, update) @gurlcode

Slide 138

Slide 138

HostRoot div h1 C div enqueueSetState(fiber, update) @gurlcode

Slide 139

Slide 139

HostRoot div h1 scheduleWork( ) C div enqueueSetState(fiber, update) @gurlcode

Slide 140

Slide 140

HostRoot div h1 C div enqueueSetState(fiber, update) @gurlcode

Slide 141

Slide 141

HostRoot div h1 C div performWork @gurlcode

Slide 142

Slide 142

HostRoot div h1 renderRoot C div performWork @gurlcode

Slide 143

Slide 143

HostRoot div h1 C workInProgress = current.alternate renderRoot div performWork @gurlcode

Slide 144

Slide 144

HostRoot div h1 nextUnitOfWork = workInProgress C div performWork @gurlcode

Slide 145

Slide 145

HostRoot div h1 C workLoop nextUnitOfWork = workInProgress div performWork @gurlcode

Slide 146

Slide 146

HostRoot div h1 C div workLoop @gurlcode

Slide 147

Slide 147

HostRoot div h1 performUnitOfWork C div workLoop @gurlcode

Slide 148

Slide 148

HostRoot div h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 149

Slide 149

HostRoot div h1 C oldProps !== newProps performUnitOfWork div workLoop @gurlcode

Slide 150

Slide 150

HostRoot div h1 C return workInProgress.child performUnitOfWork div workLoop @gurlcode

Slide 151

Slide 151

HostRoot div h1 return workInProgress.child C div workLoop @gurlcode

Slide 152

Slide 152

HostRoot div h1 C div workLoop @gurlcode

Slide 153

Slide 153

HostRoot div h1 C div @gurlcode

Slide 154

Slide 154

HostRoot div h1 C div @gurlcode

Slide 155

Slide 155

HostRoot div h1 C div workLoop @gurlcode

Slide 156

Slide 156

HostRoot div h1 performUnitOfWork C div workLoop @gurlcode

Slide 157

Slide 157

HostRoot div h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 158

Slide 158

HostRoot div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 159

Slide 159

HostRoot updateClassInstance div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 160

Slide 160

HostRoot processUpdateQueue updateClassInstance div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 161

Slide 161

HostRoot fiber.memoizedState = newState updateClassInstance div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 162

Slide 162

HostRoot div instance.state = fiber.memoizedState h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 163

Slide 163

HostRoot div nextChild = instance.render( ) h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 164

Slide 164

HostRoot reconcileChildFibers div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 165

Slide 165

HostRoot fiber.effectTag = Update div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 166

Slide 166

Fiber { work to be done alternate tag child sibling return memoizedState pendingProps effectTag … } @gurlcode

Slide 167

Slide 167

HostRoot return workInProgress.child div updateClassComponent h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 168

Slide 168

HostRoot div return workInProgress.child h1 C beginWork performUnitOfWork div workLoop @gurlcode

Slide 169

Slide 169

HostRoot div h1 C return workInProgress.child performUnitOfWork div workLoop @gurlcode

Slide 170

Slide 170

HostRoot div h1 return workInProgress.child C div workLoop @gurlcode

Slide 171

Slide 171

HostRoot div h1 C div workLoop @gurlcode

Slide 172

Slide 172

HostRoot div h1 C div @gurlcode

Slide 173

Slide 173

div h1 C div @gurlcode

Slide 174

Slide 174

f f f f @gurlcode

Slide 175

Slide 175

VDOM @gurlcode

Slide 176

Slide 176

@gurlcode

Slide 177

Slide 177

fn fn fn @gurlcode

Slide 178

Slide 178

When a function is called, a new stack frame is added fn fn fn fn @gurlcode

Slide 179

Slide 179

Fibers are virtual stack frames fiber fiber fiber fiber @gurlcode

Slide 180

Slide 180

@gurlcode

Slide 181

Slide 181

Thank You! Huge shoutout to the React core team 💛 @gurlcode