Reusing a last assignment
Reusing a value and memorizing a value sometimes refer to similar behavior. However, it's worth noting that the useMemo
hook can only remember one value from the past, the last value.
A single JavaScript variable, by default, serves a purpose that, unless overwritten by a new assignment, holds the previously assigned value. So, take caution when reading the word "memo" here. If you take the word "memo" as a single value instead of memorizing all values, it could help you visualize it the right way as React designed it. If you are interested in classical memorization, check out the Appendix A – Not a classical memorization section at the end of this chapter.
How useMemo
reuses the previous assignment is controlled by a deps
dependency array, and it uses the areDepsEqual
utility function to compare two dependency arrays between the previous and current update. We have already examined this function in Chapter 5, Use Effect...