WorryFree Computers   »   [go: up one dir, main page]

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

The art of ignoring best practices for React performance

The art of ignoring best practices for React performance

Have you ever wanted to break the rules and be a React troublemaker? I’ll show you how ignoring React’s best practices can improve performance, all without major rewrites to your codebase. We’ll break rules like unidirectional data flow, pure components, and never calling hooks conditionally. To understand how, we’ll learn when React decides to render and how to trick it to render less frequently. We’ll build components that don’t render anything, conditionally call hooks, and even use a piece of Svelte - all to quickly fix real performance problems I encountered at Microsoft.

Tiger Oakes

May 04, 2024
Tweet

More Decks by Tiger Oakes

Other Decks in Programming

Transcript

  1. Why do you need to… Use unidirectional data flow Avoid

    calling hooks conditionally Store state using the useState hook
  2. About me Senior Software Engineer building Microsoft Loop Worked on

    Edge, Chrome, and Firefox React developer for 8+ years tigeroakes.com @notwoods.bsky.social @not_woods
  3. THE PLAN 01 When does React rerender? 02 How to

    isolate expensive hooks Using impure components 03 How to only update leaf components Using a different UI library
  4. Every rerender starts with a state change. State change Component

    re-renders Descendant components re-render
  5. function MyCounter() { const [count, setCount] = useState(0); const handleClick

    = () => setCount(count + 1); return ( <div> <Title /> <CounterDisplay count={count} /> <button </div> ); } function Title() { return <h1># of rules broken</h1>; } function CounterDisplay({ count }) { return <strong>{count}</strong>; }
  6. function MyCounter() { const [count, setCount] = useState(0); const handleClick

    = () => setCount(count + 1); return ( <div> <Title /> <CounterDisplay count={count} /> <button </div> ); } function Title() { return <h1># of rules broken</h1>; } function CounterDisplay({ count }) { return <strong>{count}</strong>; }
  7. function MyCounter() { <div> <h1># of rules broken</h1> <strong>{1}</strong> <button

    </div> } <div> <h1># of rules broken</h1> <strong>{0}</strong> <button </div> vs
  8. App MyCounter state: count (0) Title CounterDisplay props: count (1)

    (2) (3) (4) Increment React.memo() React.memo()
  9. 02 Isolating expensive hooks Isolate state changes with side effect

    components Photo by Sam Pearce-Warrilow on Unsplash
  10. function useGetData() { const [state, setState] = useState(); useEffect(() =>

    { fetchData().then(setState); }, []); return state; } function App() { const data = useGetData(); return <UI data={data} />; }
  11. function useGetData() { const [state, setState] = useState(); const [otherState,

    setOtherState] = useState(); useEffect(() => { fetchData().then(setState); }, []); useEffect(() => { fetchOtherData(state.id).then(setOtherState); }, [state.id]); return otherState; } function App() { const data = useGetData(); return <UI data={data} />; }
  12. function useGetData() { const [otherState, setOtherState] = useState(); useEffect(() =>

    { fetchData() .then(state => fetchOtherData(state.id)) .then(setOtherState) }, []); return otherState; } function App() { const data = useGetData(); return <UI data={data} />; }
  13. function ExpensiveEffect(props) { const otherState = useGetData(); useEffect(() => {

    props.setState(otherState); }, [otherState]); return null; } function App() { const [data, setData] = useState(); return <> <ExpensiveEffect setState={setData} /> <UI data={data} /> </>; }
  14. const LazyExpensiveEffect = React.lazy(() => import('./ExpensiveEffect’) ); return ( <>

    <React.Suspense fallback={null}> <ExpensiveEffect setState={setData} /> </React.Suspense> <UI data={data} /> </> );
  15. @Composable fun loadData() { val (state, setState) = remember {

    mutableStateOf() } SideEffect { fetchData().then(::setState) } return state } @Composable fun App() { … }
  16. 03 Only updating leaf components Pull state management out of

    React Photo by Jakob Rosen on Unsplash
  17. import { writable } from 'svelte/store'; let store = writable(initialState);

    store.set(newState); store.update(oldState => newState);
  18. import { writable } from 'svelte/store'; function Sidebar(props) { const

    store = React.useMemo(() => writable()); return <> {props.items.map(item => <Item item={item} state={store} /> )} </> }
  19. import { useSyncExternalStore } from 'react'; import { get }

    from 'svelte/store'; // useSyncExternalStore internally uses // useState and useEffect const state = useSyncExternalStore( store.subscribe, () => get(store) );
  20. Svelte Stores • Store is a constant reference passed around

    with props or context. • Store’s value can be changed without re-rendering React tree. • Translated into React state using the useSyncExternalStore hook.
  21. function Sidebar(props) { const store = React.useMemo(() => writable()); return

    <> <ExpensiveEffect setState={store.set} /> {props.items.map(item => <Item item={item} state={store} /> )} </> }
  22. import { writable, readable } from 'svelte/store'; class DataModel {

    #state = writable(); get state() { return readable(this.#state); } performUpdate() { this.#state.set('done') } }
  23. Redux • Store is a constant reference passed around with

    <Provider />. • Store’s value can be changed without re-rendering React tree. • Translated into React state using the useSelector hook.
  24. React.useReducer • Reducer value is stored as one giant React

    state object. • Changing reducer’s value will re-render React tree. • State is translated at the top of the React tree once, instead of in the leaf components.
  25. import { readable } from 'svelte/store'; const mediaQuery = window.matchMedia('(max-width:

    640px)'); const mediaQueryStore = readable( mediaQuery.matches, function onFirstListenerStart(set) { const listener = () => set(mediaQuery.matches); listener(); mediaQuery.addEventListener('change', listener); // return cleanup function, similar to useEffect return function onLastListenerStop() { mediaQuery.removeEventListener('change', listener); } } );
  26. CREDITS: This presentation template was created by Slidesgo, and includes

    icons by Flaticon, and infographics & images by Freepik THANKS! https://loop.microsoft.com tigeroakes.com @notwoods.bsky.social @not_woods Photo by Fabio Spinelli on Unsplash