import React from 'react';
const ChildComponent = React.memo(({ name }) => {
console.log('Rendering ChildComponent');
return <div>Hello, {name}!</div>;
});
function ParentComponent() {
const [count, setCount] = useState(0);
return (
<div>
<ChildComponent name="John" />
<button onClick={() => setCount(count + 1)}>Increment Count</button>
<p>Count: {count}</p>
</div>
);
}
In this example, ChildComponent
only re-renders if the name
prop changes, even when the parent component re-renders due to state changes.