So I am just getting the hang of useMemo and useCallback and playing around to see how things are working. What I want to do in this scenario is not to render the Child whenever the parent is sending the same object.
Here is my Parent component:
const Parent = () => {
const [value, setValue] = useState(0);
const handleOnClick = e => {
setValue(value + 1);
};
const myObject = useMemo(() => {
return { value: value - value };
}, [value]);
return (
<div>
<h1>I am a parent</h1>
<button onClick={handleOnClick}>
Click on Me
</button>
<Child object={myObject} />
</div>
);
};
export default Parent;
And here is my Child which is re-rendering on each click although the content of "myObject" is the same. (I know the reference changes each time but my understanding is that's what useMemo does)
const Child = ({ object }) => {
return (
<div>
<h1>I am child</h1>
<p>Object is {JSON.stringify(object)}</p>
</div>
);
};
export default React.memo(Child);
Please let me know what am I doing wrong and what's the solution for this case?