You should always use React.memo
LITERALLY, as comparing the tree returned by the Component is always more expensive than comparing a pair of props
properties
So don’t listen to anyone and wrap ALL functional components in React.memo
. React.memo
was originally intended to be built into the core of functional components, but it is not used by default due to the loss of backward compatibility. (Since it compares the object superficially, and you MAYBE are using the nested properties of the sub-object in the component) =)
That’s it, this is the ONLY REASON why React doesn’t use memo Automatically. =)
In fact, they could make version 17.0.0, which would BREAK backward compatibility, and make React.memo
the default, and make some kind of function to cancel this behavior, for example React.deepProps
=)
Stop listening to theorists, guys =) The rule is simple:
If your component uses DEEP COMPARING PROPS then don’t use memo, otherwise ALWAYS use it, comparing TWO OBJECTS is ALWAYS CHEAPER than calling React.createElement()
and comparing two trees, creating FiberNodes, and so on.
Theorists talk about what they themselves do not know, they have not analyzed the react code, they do not understand FRP and they do not understand what they’re advising =)
P.S. if your component is using children
prop, React.memo
will not work, because children
prop always makes a new array. But It is better not to bother about this, and even such components should ALSO be wrapped in React.memo
, since the computing resources are negligible.