8,037 Matching Annotations
  1. Dec 2022
    1. ```js import React from "react"; import Dexie from "dexie"; import { useLiveQuery } from "dexie-react-hooks"; import { db } from "./db";

      // // React component // export function OldFriendsList() { const friends = useLiveQuery( () => db.friends .where('age') .above(75) .toArray() );

      if (!friends) return null; // Still loading.

      return

        { friends.map(friend =>
      • {friend.name}, {friend.age}
      • ) }
      ; } ```

    1. Modern browsers will reset Window.name to an empty string if a tab loads a page from a different domain, and restore the name if the original page is reloaded (e.g. by selecting the "back" button).
    1. Context selectors, however, could be faked with the use of higher-order components and React.memo.
    2. There is no way to prevent a component that uses a portion of Context value from re-rendering, even if the used piece of data hasn’t changed, even with useMemo hook.
    3. ✅ Preventing Context re-renders: Context selectors

    4. If Context manages a few independent data chunks, they can be split into smaller providers under the same provider. That way, only consumers of changed chunk will re-render.
    5. ✅ Preventing Context re-renders: splitting data into chunks

    6. That way, components that use API only won’t re-render when the data changes.
    7. If in Context there is a combination of data and API (getters and setters) they can be split into different Providers under the same component. That way, components that use API only won’t re-render when the data changes.
    8. ✅ Preventing Context re-renders: splitting data and API

    9. If Context Provider is placed not at the very root of the app, and there is a possibility it can re-render itself because of changes in its ancestors, its value should be memoized.
    10. ✅ Preventing Context re-renders: memoizing Provider value

    11. Preventing re-renders caused by Context
    12. ⛔️ Antipattern: random value as key in lists

    13. if the list is static, i.e. elements are not added/removed/inserted/re-ordered
    14. It is okay to use array’s index as key, if the list is static, i.e. elements are not added/removed/inserted/re-ordered.
    15. Value in key should be a string, that is consistent between re-renders for every element in the list.
    16. Important: just providing key attribute will not improve lists' performance. To prevent re-renders of list elements you need to wrap them in React.memo and follow all of its best practices.
    17. Improving re-render performance of lists

    18. the typical use case for useMemo would be to memoize React elements. Usually parts of an existing render tree or results of generated render tree, like a map function that returns new elements
    19. useMemo has its cost (consumes a bit of memory and makes initial render slightly slower)
    20. ✅ useMemo for expensive calculations

    21. If a component uses non-primitive value as a dependency in hooks like useEffect, useMemo, useCallback, it should be memoized.

    22. If a child component is wrapped in React.memo, all props that are not primitive values have to be memoized

    23. ✅ Necessary useMemo/useCallback

      <br/>

    24. ⛔️ Antipattern: unnecessary useMemo/useCallback on props

    25. If a parent component re-renders, it will trigger re-render of a child component regardless of its props.
    26. Memoizing props by themselves will not prevent re-renders of a child component.
    27. React.memo has to be applied to the elements passed as children/props.
    28. ✅ React.memo: components as props or children

      Description

    29. All props that are not primitive values have to be memoized for React.memo to work
    30. ✅ React.memo: component with props

      Description

    31. Wrapping a component in React.memo will stop the downstream chain of re-renders that is triggered somewhere up the render tree, unless this component’s props have changed.
    32. Preventing re-renders with React.memo

      Description

    33. Props are not affected by the state change, so heavy components won’t re-render
    34. encapsulates the state inside a smaller component, and heavy components are passed to it as props
    35. ✅ Preventing re-renders with composition: components as props

      Description

    36. ✅ Preventing re-renders with composition: children as props

      Description

    37. ✅ Preventing re-renders with composition: moving state down

      Description

    38. There is also a big myth: that re-renders happen when the component’s props change. By itself, it’s not true (see the explanation below).
    39. hooks changes
    40. context changes
    41. parent (or children) re-renders
    42. state changes
    43. There are four reasons why a component would re-render itself: