Our React project has a code written in Composition Pattern as below.
return (
<Template
title={
useMemo(
() => (
<>
<TitleComponent prop={value} />
<TitleComponent prop={value} />
</>
),
[deps]
)
}
/>
)
I didn’t apply useMemo to TitleComponent at first. According to this article, I thought it would be optimized just by delivering components to prop.
However, my colleague argues that it is better to use useMemo because if you apply useMemo, you can skip the render phase. Otherwise, he claim that only the commit phase is skipped.
- Is it true that render phase runs if useMemo is not used in the code above?
- Is it useful to wrap prop components in useMemo?
- I editted my code. There were multiple components wrapped with fragment in prop.
3
Answers
firstly,
useMemo
is useful if it takes time to rerender, otherwise it won’t change anything.If you don’t use it:
Template
component rerenders, thetitle
wontTemplate
rerenders, thetitle
andTemplate
will rerender as wellIf you use it:
Template
component rerenders, thetitle
wontTemplate
rerenders, theTemplate
will rerender as well, but thetitle
will rerender only if there is difference in the dependency arrayThis thing
<Component ... />
returns an object (React callscreateElement
and it returns an object).React.createElement
is different (by reference) than the other one from previous renderI’m not going to answer your questions since I think you’re confused on what really is the purpose of that hook. Instead I’ll give you a better understanding on it.
useMemo
is used when you have a very heavy computation inside a component. What that hook does is, it takes a state/props as dependencies and it wont re-execute the computations unless those dependencies change. Meaning if you have a component that does very heavy computations on render that causes your app to slow down, you need to wrap the computation inside auseMemo
to prevent recomputing your heavy calculations during rerender. In your case, do you have heavy computation on that title component? Absolutely NOT. So you are using that hook wrong.What you need is
React.memo
. This one will convert your component intoPureComponent
which will skip unnecessary rerenders if your component only displays static content that doesn’t change.Is always nice to try using
useMemo
.What it does: It memoize your component/data and rerender when any variable of the dependencies array changes. So when any other state variable changes react will not have to rerender this memoized component improving performance.
Some use cases are:
Here’s a little example of how to use it:
What is it doing here?
When you change
myVar
value usingsetMyVar
react will rerender the component to update it’s content in dom.But if you change
myOtherVar
value withsetMyOtherVar
react will not rerender the component and it’s value will not be updated in dom.PS: If you change
myOtherVar
and after this you changemyVar
both values will be updated in dom because the rerender was done after updating the unmemoized variable so react will take all updated values of all variables.Here‘s a simple codesandbox working example