Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The best is yet to come: the Future of React • FULL

The best is yet to come: the Future of React • FULL

A wise man once said: "React is such a good idea that we will spend the rest of the decade continuing to explore its implications and applications”.

In 2017, React Fiber was the thing in the community. In 2018, hooks – and the completely new mindset they brought along – took that role with a little help of Concurrent React.

But there are a few other big things happening out there: React Fire, React Flare and the Scheduler. These go from completely rethinking the event system to bringing cooperative scheduling to the browser environment, and much more!

In this talk, we’ll look at what are these, how they fit together with other changes and, hopefully, by the end of the talk, you'll be just as enthusiastic as I am about what's coming down the line.

Last but not least, we'll take a glimpse into the future of the Web platform.

Matheus Albuquerque

March 14, 2020
Tweet

More Decks by Matheus Albuquerque

Other Decks in Programming

Transcript

  1. THE BEST IS YET TO COME: THE FUTURE OF REACT

    Matheus Albuquerque – Software Engineer, Front-End @ STRV full
  2. 3 contextType createRef() forwardRef() Lifecycle Changes <Strict Mode/> act() Migrating

    Stuff ReactDOM.createRoot() lazy() <Suspense/> react-cache Profiler memo() scheduler Create React App v3 GOALS • REACT RECAP
  3. 4 contextType createRef() forwardRef() Lifecycle Changes <Strict Mode/> act() Migrating

    Stuff ReactDOM.createRoot() lazy() <Suspense/> react-cache Profiler memo() scheduler Create React App v3 ALL OF THIS STARTING ON 16.3… GOALS • REACT RECAP
  4. 5 Guillermo Rauch, CEO @ZEIT “React is such a good

    idea that we will spend the rest of the decade continuing to explore its implications and applications.“
  5. 02 PROVE THREE THEORIES I HAVE REGARDING THE WEB PLATFORM

    AND REACT.JS 8 8 GOALS no spoilers here, sorry
  6. 01 REACT SOURCE CODE IS NOT EASY TO READ AND

    IT IS CONSTANTLY CHANGING 10 10 DISCLAIMERS
  7. 15 Consists of… • An effort to modernize React DOM

    • Focused on making React better aligned with how the DOM works • It also aims to make React smaller and faster It brings… • Attach events at the React root rather than the document • Migrate from onChange to onInput and don’t polyfill it for uncontrolled components • className → class • React Flare • … DOM • REACT FIRE
  8. 16 • Attaching event handlers to the document becomes an

    issue when embedding React apps into larger systems • Any big application eventually faces complex edge cases related to stopPropagation interacting with non- React code or across React roots • The Atom editor was one of the first cases that bumped into this DOM • REACT FIRE • ATTACH EVENTS AT THE REACT ROOT
  9. 17 • Stop using a different event name for what's

    known as input event in the DOM • Stop polyfilling it for uncontrolled components DOM • REACT FIRE • ONCHANGE → ONINPUT
  10. 19 • Experimental React Events API (react-events) • Necolas is

    the creator of react-native-web and cares a lot about cross-platform consistency • The Event System umbrella under React Fire DOM • REACT FLARE
  11. 20 • Make it easy to build UIs that feel

    great on desktop and mobile, with mouse and touch, and that are accessible • It includes declarative APIs for managing interactions • Unlike with the current React event system, the Flare design doesn't inflate the bundle for events you don't use • It should allow to cut down the amount of code in UI libraries that deal with mouse and touch events DOM • REACT FLARE
  12. 21 • It includes declarative APIs for managing interactions DOM

    • REACT FLARE • useTap • useKeyboard • usePress • useResponder • useFocusManager • listeners={…}
  13. 22 useKeyboard({ preventKeys: [ 'Space', [ 'Enter', { metaKey: true

    } ] 'Enter+Meta' ] }); DOM • REACT FLARE
  14. 23 const [focused, setFocusState] = useState(false); const { onBlur, onFocus

    } = useFocusManager({ onChange: nextFocused => setFocusState(nextFocused) }); return ( <div tabIndex="-1" onFocus={onFocus} onBlur={onBlur}> {String(focused)} <input /> <input /> <button>A button</button> </div> ); DOM • REACT FLARE
  15. 24 const [focused, setFocusState] = useState(false); const { onBlur, onFocus

    } = useFocusManager({ onChange: nextFocused => setFocusState(nextFocused) }); return ( <div tabIndex="-1" onFocus={onFocus} onBlur={onBlur}> {String(focused)} <input /> <input /> <button>A button</button> </div> ); DOM • REACT FLARE
  16. 25 const [focused, setFocusState] = useState(false); const { onBlur, onFocus

    } = useFocusManager({ onChange: nextFocused => setFocusState(nextFocused) }); return ( <div tabIndex="-1" onFocus={onFocus} onBlur={onBlur}> {String(focused)} <input /> <input /> <button>A button</button> </div> ); DOM • REACT FLARE
  17. 26 • The Flare event system ended up being a

    too-high-level-abstraction • As parts of the event system considered unnecessary or outdated were removed, they discovered many edge cases where it was being very helpful and prevented bugs • Reducing library code to re-add it several times in the application code was not the best tradeoff • Even basic things like buttons feel very different with mouse and touch when you use events like onClick DOM • REACT FLARE
  18. 32 SCHEDULING • OVERVIEW • PROBLEMS IN UIs • Users

    expect immediate feedback • Events can happen at any time • We can’t look into the future
  19. 36 • Provides a scalable solution to performance • Lets

    us coordinate CPU and network-bound updates • Improves perceived performance • It doesn’t solve React.js problems; it solves UI problems SCHEDULING • OVERVIEW
  20. 39 SCHEDULING • REACT Concurrent React • Allows rendering work

    to be paused and resumed later • Makes tasks smaller Scheduler • A way to schedule work in the browser • Unstable! API will change
  21. 42 SCHEDULING • REACT Immediate User Blocking Normal Low Idle

    "Do it now" Now! "Do it now" 250ms "Do it soon" 5s "Do it eventually” 10s "Do it if you can” No timeout
  22. 43 SCHEDULING • REACT Immediate User Blocking Normal Low Idle

    "Do it now" Now! "Do it now" 250ms "Do it soon" 5s "Do it eventually” 10s "Do it if you can” No timeout first one is really sync
  23. 48 ReactDOM.render( CONCURRENT_AND_SCHEDULED ? ( <React.unstable_ConcurrentMode> <App /> </React.unstable_ConcurrentMode> )

    : ( <App /> ), rootElement ); SCHEDULING • REACT • DEMO Enabling the unstable Concurrent Mode
  24. 49 const handleChange = useCallback(event => { const value =

    event.target.value; if (CONCURRENT_AND_SCHEDULED) { setInputValue(value); unstable_next(function() { onChange(value); }); sendDeferredPing(value); } else { setInputValue(value); onChange(value); sendPing(value); } }); SCHEDULING • REACT • DEMO
  25. 50 const handleChange = useCallback(event => { const value =

    event.target.value; if (CONCURRENT_AND_SCHEDULED) { setInputValue(value); unstable_next(function() { onChange(value); }); sendDeferredPing(value); } else { setInputValue(value); onChange(value); sendPing(value); } }); SCHEDULING • REACT • DEMO Queue a task with a lower priority than the default priority of interaction callbacks.
  26. 51 const handleChange = useCallback(event => { const value =

    event.target.value; if (CONCURRENT_AND_SCHEDULED) { setInputValue(value); unstable_next(function() { onChange(value); }); sendDeferredPing(value); } else { setInputValue(value); onChange(value); sendPing(value); } }); SCHEDULING • REACT • DEMO
  27. 52 const handleChange = useCallback(event => { const value =

    event.target.value; if (CONCURRENT_AND_SCHEDULED) { setInputValue(value); unstable_next(function() { onChange(value); }); sendDeferredPing(value); } else { setInputValue(value); onChange(value); sendPing(value); } }); SCHEDULING • REACT • DEMO
  28. 53 const sendPing = value => { performance.mark("analytics-start"); someRandomOperation(25); performance.mark("analytics-end");

    performance.measure( "Analytics: " + value, "analytics-start", "analytics-end" ); }; const sendDeferredPing = value => { unstable_scheduleCallback(unstable_LowPriority, function() { sendPing(value); }); }; SCHEDULING • REACT • DEMO
  29. 54 const sendPing = value => { performance.mark("analytics-start"); someRandomOperation(25); performance.mark("analytics-end");

    performance.measure( "Analytics: " + value, "analytics-start", "analytics-end" ); }; const sendDeferredPing = value => { unstable_scheduleCallback(unstable_LowPriority, function() { sendPing(value); }); }; SCHEDULING • REACT • DEMO We can schedule a callback with an even lower priority.
  30. 57 SCHEDULING • REACT • DEMO Demo recap • Concurrent

    React can break long running tasks into chunks • The scheduler allows us to prioritize important updates
  31. 59 SCHEDULING • THE WEB • Everyone should use the

    same scheduler • Having more than one scheduler causes resource fighting • Interleaving tasks with browser work (rendering, GC)
  32. 60 SCHEDULING • THE WEB We have a few scheduling

    primitives: • setTimeout • requestAnimationFrame • requestIdleCallback • postMessage
  33. 64 SCHEDULING • THE WEB • Developed in cooperation with

    React, Polymer, Ember, Google Maps, and the Web Standards Community • Aligned with the work of the React Core Team • Integrated directly into the event loop
  34. 69 while (workQueue.length > 0) { if (navigator.scheduling.isInputPending()) { //

    Stop doing work if we have to handle an input event. break; } let job = workQueue.shift(); job.execute(); } SCHEDULING • THE WEB
  35. 70 while (workQueue.length > 0) { if (navigator.scheduling.isInputPending(['mousedown', 'keydown'])) {

    // Stop doing work if we think we'll start receiving a mouse or key event. break; } let job = workQueue.shift(); job.execute(); } SCHEDULING • THE WEB
  36. 71 SCHEDULING • CONCLUSIONS • Scheduling is necessary for responsive

    user interfaces • We can solve a lot at the framework level with Concurrent React and the Scheduler • A Web Standards proposal is in making that brins a scheduler API to the browser
  37. 78 CONTROL FLOW • EFFECT HANDLERS function getName(user) { let

    name = user.name; if (name === null) { name = perform 'ask_name'; } return name; } const arya = { name: null, friendNames: [] }; const gendry = { name: 'Gendry', friendNames: [] }; try { getName(arya); } handle (effect) { if (effect === 'ask_name') { resume with 'Arya Stark'; } }
  38. 79 CONTROL FLOW • EFFECT HANDLERS function getName(user) { let

    name = user.name; if (name === null) { name = perform 'ask_name'; } return name; } const arya = { name: null, friendNames: [] }; const gendry = { name: 'Gendry', friendNames: [] }; try { getName(arya); } handle (effect) { if (effect === 'ask_name') { resume with 'Arya Stark'; } } throw ↝ perform catch ↝ handle lets us jump back to where we performed the effect
  39. 87 CONTROL FLOW • REACT The React team apparently spent

    some time experimenting with using effect-handler control structures for managing layout
  40. 94 CONTROL FLOW • REACT A component is able to

    suspend the fiber it is running in by throwing a promise, which is caught and handled by the framework.
  41. 95 CONTROL FLOW • REACT A component is able to

    suspend the fiber it is running in by throwing a promise, which is caught and handled by the framework. throw-handle-resume pattern
  42. 96 2016 2016 2017 2018 2019 Effect Handlers as ECMAScript

    proposal CONTROL FLOW • REACT Effect Handlers experiments (Layout) Effect Handlers experiments (Context) Effect Handlers as Hooks Effect Handlers as Suspense
  43. REACT.JS HAS BEEN PUSHING WEB APIs TO THE FUTURE 97

    97 CONTROL FLOW • CONCLUSIONS
  44. 102 • Help obliterate DOM XSS • Allow you to

    lock down the dangerous injection sinks OTHER COOL STUFF • TRUSTED TYPES
  45. 106 • It's a reimplementation of hot reloading with full

    support from React • It's originally shipping for React Native but most of the implementation is platform-independent • The plan is to use it across the board — as a replacement for purely userland solutions (like react-hot-loader) • Adoption requires integration with existing bundlers common on the web (e.g. Webpack, Parcel etc.) OTHER COOL STUFF • FAST REFRESH
  46. 109 • A new generation of hot reloading • Changes

    include initial scaffolding, infrastructure, and Babel plugin implementation OTHER COOL STUFF • REACT FRESH
  47. 115 • ”An experimental API aimed to greatly improve server

    side rendering experience” – Philipp Spiess • ”A non-GraphQL solution for composing arbitrary data logic in a parallel hierarchy to your components” – Dan Abramov • Probably somehow related to mapping URLs to data and views (similar to Navi) OTHER COOL STUFF • REACT FLIGHT
  48. 01 REACT.JS HAS BEEN PUSHING OTHER ECOSYSTEMS TO THE FUTURE

    124 124 e.g. Swift UI and other declarative-UIs efforts CONCLUSIONS • THEORIES
  49. 02 REACT.JS HAS BEEN PUSHING WEB APIs TO THE FUTURE

    125 125 e.g. Scheduler API, Fast Refresh, React Fresh, Trusted Types & Effect Handlers CONCLUSIONS • THEORIES
  50. REACT.JS HAS BEEN PUSHED BY WEB APIs TO THE FUTURE

    126 126 e.g. React Fire and React Flare, Trusted Types 03 CONCLUSIONS • THEORIES
  51. 127 Guillermo Rauch, CEO @ZEIT “React is such a good

    idea that we will spend the rest of the decade continuing to explore its implications and applications.“
  52. THE BEST IS YET TO COME THE FUTURE OF REACT

    Matheus Albuquerque – Software Engineer, Front-End @ STRV
  53. THE BEST IS YET TO COME THE FUTURE OF REACT

    Matheus Albuquerque – Software Engineer, Front-End @ STRV IS
  54. THE BEST IS YET TO COME THE FUTURE OF REACT

    Matheus Albuquerque – Software Engineer, Front-End @ STRV IS
  55. THE BEST IS YET TO COME THE FUTURE OF REACT

    Matheus Albuquerque – Software Engineer, Front-End @ STRV IS NOT
  56. 141 141 This is me giving a talk about Ionic

    on an iOS developers meetup five years ago telling them that Angular would be the future. CONCLUSIONS • THE PAST
  57. 142 142 This is me giving a talk about Ionic

    on an iOS developers meetup five years ago telling them that Angular would be the future. CONCLUSIONS • THE PAST
  58. 143 143 This is me giving a talk about Ionic

    on an iOS developers meetup five years ago telling them that Angular would be the future. CONCLUSIONS • THE PAST
  59. 144 144 This is me giving a talk about Ionic

    on an iOS developers meetup five years ago telling them that Angular would be the future. CONCLUSIONS • THE PAST