Consider example:
const Child = () => {
console.log("I did re-render!");
return null;
};
const App = () => {
const [_, setValue] = useState();
const fn = useCallback(() => {
// do something
}, []);
return (
<div>
<button onClick={() => setValue(Math.random)}>click</button>
<Child fn={fn} />
</div>
);
};
With every state change in App
(click the button), the Child
component re-renders, even if the passed prop fn
is wrapped with useCallback
. However, if I wrap Child
with React.memo
, it starts to work correctly - it does not re-render when parent re-renders.
My question: What's the point of using useCallbacks
without React.memo
?? Should I always use React.memo
if I dont want the component to always re-render if its parent re-renders?
Should useCallbacks
always be used with React.memo
? Because it seems like they are senseless and useless without React.memo
.