Slide 1

Slide 1 text

What’s new in React 16?

Slide 2

Slide 2 text

React 16 Complete rewrite of it’s core algorithm Incremental Rendering React Fiber Supports better animations, layout & gestures Fully backward compatible Pause, resume, abort, reuse work Prioritization Async

Slide 3

Slide 3 text

What problem are we trying to solve?

Slide 4

Slide 4 text

What happens in a frame? • Evaluating JavaScript • Calculating styles • Laying out elements • Painting the actual pixels • Most screens refresh 60 times per second (60fps), • This window represents the time to get a single frame to the screen (Professor Math says 1000 ÷ 60 = ~16ms).

Slide 5

Slide 5 text

Fps demo

Slide 6

Slide 6 text

Stack Reconciler

Slide 7

Slide 7 text

Stack Reconciler

Slide 8

Slide 8 text

Stack Reconciler In its current React walks the tree recursively and calls render functions of the whole updated tree dimplementationuring a single tick.

Slide 9

Slide 9 text

How do we achieve 60fps? Scheduling Prioritizing +

Slide 10

Slide 10 text

Scheduling Specifically, being be able to 1. Pause work and come back to it later. 2. Assign priority to different types of work. 3. reuse previously completed work. 4. abort work if it's no longer needed. In order to do any of this, we first need a way to break work down into units. In one sense, that's what a fiber is. A fiber represents a unit of work.

Slide 11

Slide 11 text

Prioritizing React takes control of our work and assigns priorities • User events (clicks, input change) • External data subscriptions (redux) • Animations (transitions/gestures) • Updates to offscreen content.

Slide 12

Slide 12 text

Goal 1. Interrupt the current, lower priority work. 2. Complete the high priority work. 3. Resume the interrupted work where it left off. But, how do you interrupt a function call in JavaScript?

Slide 13

Slide 13 text

Let’s come up with a plan Here’s one way to do this • Interrupt current rendering call stack • "Stash" the call stack off to the side • Perform some higher priority work, which has its own call stack • Go back to the original call stack and resume. This is essentially what React Fiber does. Fiber can switch between limitless number of call stack. It's a reimplementation of the stack, specialized for React components.

Slide 14

Slide 14 text

Fiber A fiber is just a JS object – a virtual stack frame. Information from the stack is preserved in memory on the heap. Hence, they can be manipulated and run whenever they want. Fiber contains information about a component, its input and output.

Slide 15

Slide 15 text

Fiber

Slide 16

Slide 16 text

Fiber

Slide 17

Slide 17 text

Fiber

Slide 18

Slide 18 text

Fiber

Slide 19

Slide 19 text

Fiber

Slide 20

Slide 20 text

Fiber

Slide 21

Slide 21 text

Fiber reconciliation

Slide 22

Slide 22 text

Fiber reconciliation

Slide 23

Slide 23 text

Fiber reconciliation

Slide 24

Slide 24 text

Fiber reconciliation

Slide 25

Slide 25 text

Fiber reconciliation

Slide 26

Slide 26 text

Fiber reconciliation

Slide 27

Slide 27 text

Fiber reconciliation

Slide 28

Slide 28 text

Fiber reconciliation

Slide 29

Slide 29 text

Fiber reconciliation

Slide 30

Slide 30 text

Fiber reconciliation

Slide 31

Slide 31 text

Stack vs Fiber reconciliation Stack reconciliation Updates must be completed entirely before returning to main thread. Fiber reconciliation Updates will be batched in chunks and React will manage the thread.

Slide 32

Slide 32 text

Async Rendering A key benefit of async rendering is that large updates don't block the main thread - instead, the work is spread out and performed during idle periods using cooperative scheduling. But once you make something async, you introduce the possibility that things may appear on the screen at separate times. How does React solve this?

Slide 33

Slide 33 text

Phased Rendering You can think of React rendering as being split into two main phases: • The render phase, where we compare the new tree with the existing tree and calculate the changes that need to be made. This phase is free of side-effects, so it can be done asynchronously. The work can also be interrupted, rebased, restarted, and interleaved with other updates. • the commit phase, where the changes computed during the render phase are flushed to the screen. This phase is typically very fast and is performed synchronously to avoid inconsistencies.

Slide 34

Slide 34 text

API for controlling Async Rendering

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Fragments React render() method can now return • React Elements • Arrays, Strings and numbers • null

Slide 38

Slide 38 text

Error Boundaries Are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the crashed component tree.

Slide 39

Slide 39 text

Portals Render children into a DOM node that exists in some other DOM hierarchy Eg. modals, tooltips etc.

Slide 40

Slide 40 text

New Context API Context provides a way to pass data through the component tree without having to pass props down manually at every level.

Slide 41

Slide 41 text

Lifecycle Method Deprecations

Slide 42

Slide 42 text

Lifecycle methods updated

Slide 43

Slide 43 text

More… • Streaming mode for SSR • React Strict mode • React Async Mode in depth • Create ref and forward ref API • React Suspense (shhh…!)

Slide 44

Slide 44 text

Thank you J