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

Building modern React applications

Glenn Reyes
November 09, 2019

Building modern React applications

With the new additions to React, the way we build frontend applications is evolving, leading to improved performance and maintainability. As of today, the most noticeable React API additions are Context, Hooks and Suspense.
How do we apply new best practices to manage our application state?
What are common techniques for fetching data?
How do we architect modern React applications using a type system and a design system?
In this talk, we'll walk through emerging patterns that helps us write exquisite React applications today.

Glenn Reyes

November 09, 2019
Tweet

More Decks by Glenn Reyes

Other Decks in Programming

Transcript

  1. Vincent van Gogh “Great things are done by a series

    of small things brought together.” @glnnrys
  2. Darrin Alfred In its simplest form, the term modern implies

    the up-to-date, a current development, or a spirit in step with its own time. @glnnrys
  3. Don't reinvent the wheel No need to set up and

    configure from scratch @glnnrys
  4. const ThemeContext = React.createContext('light'); const App = () => (

    <ThemeContext.Provider value="dark"> <ProfileHeader /> <Content /> <Footer /> </ThemeContext.Provider> ); const Header = () => ( <div> <ThemedButton /> </div> ); const ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); }
  5. const ThemeContext = React.createContext('light'); const App = () => (

    <ThemeContext.Provider value="dark"> <Header /> <Content /> <Footer /> </ThemeContext.Provider> ); const Header = () => ( <div> <ThemedButton /> </div> ); const ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); } const ThemeContext = React.createContext('light');
  6. const ThemeContext = React.createContext('light'); const App = () => (

    <ThemeContext.Provider value="dark"> <Header /> <Content /> <Footer /> </ThemeContext.Provider> ); const Header = () => ( <div> <ThemedButton /> </div> ); const ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); } const App = () => ( <ThemeContext.Provider value="dark"> <Header /> <Content /> <Footer /> </ThemeContext.Provider> );
  7. const ThemeContext = React.createContext('light'); const App = () => (

    <ThemeContext.Provider value="dark"> <Header /> <Content /> <Footer /> </ThemeContext.Provider> ); const Header = () => ( <div> <ThemedButton /> </div> ); const ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); } const ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); }
  8. <App /> <Header /> <Content /> <Footer /> <Articles />

    ... <ThemedButton /> ... ... ... ... <ThemeContext.Provider /> @glnnrys
  9. <App /> <Header /> <Content /> <Footer /> <Articles />

    ... <ThemedButton /> ... ... ... ... <ThemeContext.Provider /> @glnnrys
  10. import React from 'react'; function Example() { const [count, setCount]

    = React.useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); }
  11. Use hooks for new components If the team is fine

    with it Keep existing class components Rewrite them into hooks if it makes life easier @glnnrys
  12. React Docs Concurrent Mode is a set of new features

    that help React apps stay responsive and gracefully adjust to the user’s device capabilities and network speed. @glnnrys
  13. const HeavyComponent = React.lazy(() => import('./HeavyComponent')); const App = ()

    => ( <React.Suspense fallback={<Loading />}> <div> <HeavyComponent /> </div> </React.Suspense> );
  14. import React from 'react'; function Example() { const [count, setCount]

    = React.useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } Hooks @glnnrys
  15. // Class component componentDidMount() { fetchData() .then(data => this.setState({ data,

    loading: false })) .catch(error => this.setState({ error, loading: false })); } Fetch-On-Render @glnnrys
  16. // Class component componentDidMount() { fetchUser() .then(data => this.setState({ data,

    loading: false })) .catch(error => this.setState({ error, loading: false })); } // Function component useEffect(() => { fetchData() .then(data => setState({ data, loading: false })) .catch(error => setState({ error, loading: false })); }, []); Fetch-On-Render @glnnrys
  17. What's happening? Tweet Fetch-On-Render @glnnrys Download Code Fetch User Data

    Render Loading Fetch Feed Data Render Loading Render User Data
  18. What's happening? Tweet Fetch-On-Render @glnnrys Download Code Fetch User Data

    Render Loading Fetch Feed Data Render Loading Render User Data Render Feed Data Julian Krispel Btw the design of zettel is still very much evolving, if you have opinions lmk Here are some of mine Kyle Boss Who else is here at the #JSFest here in Kyiv, Ukraine? Today I will be sharing how the Web Team at @Tinder created a WordPress blog using #Javascript instead of PHP!
 Hint: we are using @gatsbyjs Alexandre Gomes Using @GraphQL + @typescript 3.7 beta optional chaining on a pet project Short-circuits deeply-accessed properties as soon as `null/undefined` is encountered!
  19. // Function component useEffect(() => { Promise.all([ fetchUser(), fetchFeed(), ])

    .then(data => this.setState({ data, loading: false })) .catch(error => this.setState({ error, loading: false })); }, []); Fetch-Then-Render @glnnrys
  20. What's happening? Tweet Fetch-Then-Render @glnnrys Download Code Fetch Feed Data

    Render Loading Fetch User Data Render Feed Data Render User Data Julian Krispel Btw the design of zettel is still very much evolving, if you have opinions lmk Here are some of mine Kyle Boss Who else is here at the #JSFest here in Kyiv, Ukraine? Today I will be sharing how the Web Team at @Tinder created a WordPress blog using #Javascript instead of PHP!
 Hint: we are using @gatsbyjs Alexandre Gomes Using @GraphQL + @typescript 3.7 beta optional chaining on a pet project Short-circuits deeply-accessed properties as soon as `null/undefined` is encountered!
  21. Render-As-You-Fetch Render Render @glnnrys Download Code Fetch Feed Data Fetch

    User Data What's happening? Tweet Julian Krispel Btw the design of zettel is still very much evolving, if you have opinions lmk Here are some of mine Kyle Boss Who else is here at the #JSFest here in Kyiv, Ukraine? Today I will be sharing how the Web Team at @Tinder created a WordPress blog using #Javascript instead of PHP!
 Hint: we are using @gatsbyjs Alexandre Gomes Using @GraphQL + @typescript 3.7 beta optional chaining on a pet project Short-circuits deeply-accessed properties as soon as `null/undefined` is encountered! Render Feed Data Render User Data
  22. Render-As-You-Fetch @glnnrys Download Code Render Render Fetch Feed Data Fetch

    User Data Render Loading Render Loading What's happening? Tweet
  23. Render-As-You-Fetch @glnnrys Download Code Fetch Feed Data Render Loading Fetch

    User Data Render Render Render Feed Data Julian Krispel Btw the design of zettel is still very much evolving, if you have opinions lmk Here are some of mine Kyle Boss Who else is here at the #JSFest here in Kyiv, Ukraine? Today I will be sharing how the Web Team at @Tinder created a WordPress blog using #Javascript instead of PHP!
 Hint: we are using @gatsbyjs Render Loading Alexandre Gomes Using @GraphQL + @typescript 3.7 beta optional chaining on a pet project Short-circuits deeply-accessed properties as soon as `null/undefined` is encountered! What's happening? Tweet
  24. What's happening? Tweet Render-As-You-Fetch @glnnrys Download Code Fetch Feed Data

    Render Loading Fetch User Data Render Feed Data Render Render Render User Data Julian Krispel Btw the design of zettel is still very much evolving, if you have opinions lmk Here are some of mine Kyle Boss Who else is here at the #JSFest here in Kyiv, Ukraine? Today I will be sharing how the Web Team at @Tinder created a WordPress blog using #Javascript instead of PHP!
 Hint: we are using @gatsbyjs Render Loading Alexandre Gomes Using @GraphQL + @typescript 3.7 beta optional chaining on a pet project Short-circuits deeply-accessed properties as soon as `null/undefined` is encountered!
  25. // example theme.js export default { breakpoints: ['40em', '52em', '64em'],

    fontSizes: [ 12, 14, 16, 20, 24, 32, 48, 64 ], colors: { blue: '#07c', lightgray: '#f6f6ff' }, space: [ 0, 4, 8, 16, 32, 64, 128, 256 ], fonts: { body: 'system-ui, sans-serif', heading: 'inherit', monospace: 'Menlo, monospace', }, fontWeights: { body: 400, heading: 700, }, buttons: { primary: { color: 'white', bg: 'primary', } } } <Button variant="primary" mr={2}>Beep</Button> <Button variant="secondary">Boop</Button>
  26. How do we architect modern React applications using a type

    system and a design system? @glnnrys