I was trying to find a concise answer to this on the web without luck.
Is the following correct regarding the differences between useEffect
, useMemo
and useState
?
useState
and useMemo
will remember a value across renders. The difference is that:
useMemo
does not cause a re-render, while useState
doesuseMemo
only runs when its dependencies (if any) have changed, while setSomeState
(second array item returned by useState
) does not have such a dependency arrayuseMemo
and useEffect
only runs when their dependencies change (if any). The difference is that:
useEffect
runs after a render happens, while useMemo
runs before
Any other key differences I have missed?
If you need to pass a state that changes to the component, you can specify it in the updates array, which is the second argument of the useMemo hook, similar to useEffect. The component will only be re-rendered when the state changes.
The useState hook is used for storing variables that are part of your application's state and will change as the user interacts with your website. The useEffect hook allows components to react to lifecycle events such as mounting to the DOM, re-rendering, and unmounting.
This means, useMemo is more urgent, and then useLayoutEffect and the last being useEffect .
These two React hooks, useMemo and useCallback are useful when you're dealing with expensive operations (that is, operations that are very complex and take a lot of time and resources, like CPU.)
Your points are basically correct, some minor clarification:
useState is causing a re-render on the call of the setState method (second element in the array returned). It does not have any dependencies like useMemo or useEffect.
useMemo only recalculates a value if the elements in its dependency array change (if there are no dependencies - i.e. the array is empty, it will recalculate only once). If the array is left out, it will recalculate on every render. Calling the function does not cause a re-render. Also it runs during the render of the component and not before.
useEffect is called after each render, if elements in its dependency array have changed or the array is left out. If the array is empty, it will only be run once on the initial mount (and unmount if you return a cleanup function).
You can always check Hooks API Reference, which is a pretty solid documentation in my opinion
useEffect(callback, [dependency])
is void
and It executes after render()
.useMemo(callback, [dependency])
is NOT void
but memoized value and It executes DURING render()
.useEffect()
can give same optimization as of useMemo() under the following circumstances:
const [count1, setCount1] = useState(0);
const [expensiveValue, setExpensiveValue] = useState(null);
useEffect(() => {
console.log("I am performing expensive computation");
setExpensiveValue(((count1 * 1000) % 12.4) * 51000 - 4000);
}, [count1]);
render()
while useMemo() makes the value available during the render()
.useEffect()
and useMemo()
both will make the value availble before browser finishes painting.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With