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

Building modern React applications

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

October 05, 2019
Tweet

More Decks by Glenn Reyes

Other Decks in Programming

Transcript

  1. 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.
  2. 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} /> ); }
  3. 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');
  4. 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> );
  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 ThemedButton = () => { const theme = React.useContext(ThemeContext); return ( <Button theme={theme} /> ); }
  6. <App /> <Header /> <Content /> <Footer /> <Articles />

    ... <ThemedButton /> ... ... ... ... <ThemeContext.Provider />
  7. <App /> <Header /> <Content /> <Footer /> <Articles />

    ... <ThemedButton /> ... ... ... ... <ThemeContext.Provider />
  8. 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> ); }
  9. 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
  10. const HeavyComponent = React.lazy(() => import('./HeavyComponent')); const App = ()

    => ( <React.Suspense fallback={<Loading />}> <div> <HeavyComponent /> </div> </React.Suspense> );
  11. 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
  12. class App extends React.Component { constructor(props) { super(props); this.state =

    { data: null, error: null, loading: false, }; } componentDidMount() { this.setState({ loading: true }); fetch('https://api.fromsomewhere.com') .then(response => response.json()) .then(data => this.setState({ data, loading: false })) .catch(error => this.setState({ error, loading: false })); } // ... }
  13. function App() { const [data, setState] = useState({ data: null,

    error: null, loading: false }); useEffect(() => { setState({ loading: true }); fetch('https://api.fromsomewhere.com') .then(response => response.json()) .then(data => setState({ data, loading: false })) .catch(error => setState({ error, loading: false })); }, []); // ... }
  14. // 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>