import React, { render } from 'react';
const NonPureComponent = ({ message }) => {
console.log('rendered');
return <div>{message}</div>;
};
const ParentComponent = () => {
const [count, setCount] = React.useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>
Increment Count: {count}
</button>
<NonPureComponent message="Hello from Non-Pure Component!" />
</div>
);
};
render(<ParentComponent />);
body, button {
font-size: 24px;
}
#root > div {
padding: 16px;
}
import React, { render, memo } from 'react';
let PureComponent = ({ message }) => {
console.log('rendered');
return <div>{message}</div>;
};
PureComponent = memo(PureComponent)
const ParentComponent = () => {
const [count, setCount] = React.useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>
Increment Count: {count}
</button>
<PureComponent
message="Hello from Pure Component!"
/>
</div>
);
};
render(<ParentComponent />);
body, button {
font-size: 24px;
}
#root > div {
padding: 16px;
}