This is common explaination about useMemo hook in React.js
useMemo(compute, dependencies) invokes compute, memoizes the calculation result, and returns it to the component.
If during next renderings the dependencies don’t change, then useMemo() doesn’t invoke compute but returns the memoized value.
- Dmitri Pavlutin
However, I saw the example of useMemo hooks that returns an array like this below.
React.useMemo(() => [1, 2, 3], []);
As you can see, useMemo returns an array which is considered as an object in Javascript.
This is confusing. I thought usage of useMemo hook is for memoizing calculated value.
So I did a little test to figure out how useMemo with object works
const Child = React.memo(({ obj }) => {
console.log('this is child component')
return <div>Child Component</div>
})
const App = () => {
const [toggle, setToggle] = React.useState(false)
const memoObj = React.useMemo(() => { return { bar: 'foo' } }, [])
return (
<div>
<button onClick={() => setToggle(!toggle)}>test</button>
<Child obj={memoObj} />
</div>
);
}
When memoObj is not wrapped by useMemo, whenever I click test button, I can see the message from Child component in console.
On the other hand, when memoObj is wrapped by the hook, the message shows up only when Child component rendered first time.
Does it mean that useMemo not only memoize calculated value but also reference of an object as well?