Answer the question
In order to leave comments, you need to log in
Use case for useLayoutEffect + useState vs useMemo?
I saw this answer: useMemo vs. useEffect + useState and it fails useEffect well, but in my case I want to perform an expensive operation that changes the DOM as early as possible. Would useMemo() still be recommended instead of useLayoutEffect() for state updates? Does double effect rendering -> state update reflect any performance gain?
useLayoutEffect() script:
useLayoutEffect(() => {
const tokens = expensiveOperationGeneratingClasses(param1)
setTokens(tokens)
},
[param1])
render (
<>
{
tokens.map(token => <span className={token.id}/>)
}
</>
)
const tokens = useMemo(() => {
return expensiveOperationGeneratingClasses(param1)
},
[param1]
render (
<>
{
tokens.map(token => <span className={token.id}/>)
}
</>
)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question