我正在重构我们的一些组件,所以我正在尝试合并 memoization,因为某些组件可能会使用相同的值重新呈现(例如,热链接的图像 URL,除非它们相同)。
我有一个简单的组件:
const CardHeader = props => {
// img is a stringand showAvatar is a boolean but it's always true
const { ..., showAvatar, img } = props;
return (
<CardHeader>
<ListItem>
// AvatarImage shouldn't re-render if img is the same as previous
{showAvatar && <AvatarImage img={img} />
</ListItem>
</CardHeader>
);
}
Run Code Online (Sandbox Code Playgroud)
然后是AvatarImage:
const AvatarImage = React.memo(props => {
console.log("why is this still re-rendering when the img value hasn't changed?");
const { img …Run Code Online (Sandbox Code Playgroud)