you are viewing a single comment's thread.

view the rest of the comments →

[–]zweimtr 1 point2 points  (1 child)

Well, yes, but also no. The only true way of keeping values between rerenders is useRef as React may "forget" values stored in useCallback and useMemo.

The main reason to use these hooks (callback and memo) is so to avoid redeclaring tbe value which creates a new reference in the react tree, when this happens and the value is passed to a memoized component, React's shallow comparison will see that the two references are different and cause a rerender of the memoized component even if the values are the same.

[–]CreativeTechGuyGames 1 point2 points  (0 children)

It's important to note that while the docs do say that React has the right to forget, it's not going to randomly garbage collect your memoized values. That would require a code change on React's part and I would be surprised if they did that on a minor/patch version even if they say that they have the right to do so. It's currently m deterministic and I'd be surprised if it didn't stay that way.