P粉1460805562023-08-26 16:13:29
Let’s talk about the conclusion first, it’s completely useless.
Why? BecausemetricsState.myMetrics
is just a value
process and does not involve expensive calculations.
But useMemo
itself consumes a certain amount of calculation.
So I think this is premature optimization
P粉7261339172023-08-26 09:26:32
useMemo
Use for expensive calculations where you don't want to run every render. like
const some = useMemo(()=> megaBigArray.reduce((acc,i)=>acc*i,0), [megaBigArray])
or something similar. You only evaluate this variable when megaBigArray
changes.
In your case, the code will run on every render anyway, but useSelector
should only trigger the render when the part of the store you selected changes. So you should be able to get by just fine without it.