Is there a benefit to removing existing memoization code? #16
-
I couldn't find any official recommendation on what to do with existing uses of useMemo / useCallback / React.memo after enabling the compiler. Is there any pros/cons to removing them? Or will it always compile to the same thing. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It is generally safe to leave manual memoization in your code – the compiler understands it but will compile in its own lower-level form of memoization even for manually memoized code. The compiler does however check its memoization against manual memoization to ensure that we memoized it exactly the same. The reason we need to do this is in case the manual memoization was used for correctness. For example, if a manually memoized value was passed as a dependency to a For this reason, our current recommendation is to leave manual memoization in for existing code, but for new code written after the compiler is added you can omit them. There is ongoing research and work being done at the moment for a long term solution to effects. Our team will share more when we have made progress on that front! |
Beta Was this translation helpful? Give feedback.
It is generally safe to leave manual memoization in your code – the compiler understands it but will compile in its own lower-level form of memoization even for manually memoized code. The compiler does however check its memoization against manual memoization to ensure that we memoized it exactly the same. The reason we need to do this is in case the manual memoization was used for correctness. For example, if a manually memoized value was passed as a dependency to a
useEffect
call somewhere else and it wasn't memoized in the same way, this might cause over/under firing of effects or even infinite loops.For this reason, our current recommendation is to leave manual memoization in for exi…