Questions
Here are some questions and answers to refresh your knowledge:
- What is
useMemo
?A
useMemo
hook is an assignment statement where a new value is created when one of the dependencies changes. It can be used to minimize the creation of a value, so it behaves like that sometimes when an assignment is "skipped." - What's the common usage of
useMemo
?It's mainly used as an optimization to avoid heavy operation on every render otherwise. If a certain evaluation is excessively used, thus blocking the UI, it is the right time to think about using
useMemo
to limit the usage to only relevant conditions. For instance, if typing isn't related to that task, we can take it out of the dependency list. - How do you use
useMemo
for memorization?useMemo
doesn't remember all past values and only remembers the last created value. So, the best usage of it is to use it as a special assignment replacement, instead of a caching mechanism.