React Flashcards
Why might a react app run slow?
Causes for latency in a react app:
- state being held too high, because this causes all children components to re-render whenever state changes
- expensive functions, need to implement useMemo
what is the useMemo hook used for?
useMemo is a React hook that memorizes the output of a function. useMemo accepts two arguments: a function and a list of dependencies. useMemo will call the function and return its return value. Then, every time you call useMemo again, it will first check if any dependencies have changed. If not, it will return the cached return value, not calling the function. If they have changed, useMemo will call the provided function again and repeat the process.
This should remind you of the useEffect hook: both useMemo and useEffect accept lists of dependencies. The only difference is that useEffect is intended for side-effects (hence the name), while functions in useMemo are supposed to be pure and with no side-effects.