const memoizedCallback = useCallback(() => {
// Your function logic here
}, [dependencies]);
import React, { render, useState, useCallback } from 'react';
const Child = React.memo(({ onClick }) => {
console.log("Child rendered");
return <button onClick={onClick}>Click Me</button>;
});
const Parent = () => {
const [count, setCount] = useState(0);
const handleClick = () => {
console.log("Button clicked");
};
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<Child onClick={handleClick} />
</div>
);
};
render(<Parent />)
body {
padding: 16px;
font-size: 24px;
}
h1 {
font-size: 32px;
}
import React, { render, useState, useCallback } from 'react';
const Child = React.memo(({ onClick }) => {
console.log("Child rendered");
return <button onClick={onClick}>Log Count</button>;
});
const Parent = () => {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
console.log(`Count: ${count}`);
}, [count]); // Recreate the function only if `count` changes
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<Child onClick={handleClick} />
</div>
);
};
render(<Parent />)
body {
padding: 16px;
font-size: 24px;
}
h1 {
font-size: 32px;
}