Should you use useMemo

First, remember that useMemo has its own overhead.

It is not useCallback, useMemo package, performance improved.

So:

What do I need to remember?

  • Expensive functions to remember.
    • So which ones are the most expensive?
      • Array. The map Array. ForEach? No, the functions themselves are optimized
      • Create a function? No, modern browsers are tiny
      • CloneDeep? If you copy a large and deep level of data, it is expensive
  • Reference type values, and:
    • Reference values passed to the downstream component as props affect the re-rendering of the child component and need to be remembered
    • As dependencies, used in other hooks, need to be remembered

What don’t you need to remember?

  • Primitive type value, no need to remember
    • If the string is/Boolean/bumber/symbol/undefined this kind of original value, there is no ‘reference’ concept, each calculated values are equal, also don’t need to remember, can remove useMemo completely.
  • Reference types that are only used within components, object/array/function, etc., do not need to be remembered

references

  • zhuanlan.zhihu.com/p/85969406
  • Blog.logrocket.com/rethinking-…