小编use*_*.io的帖子

React.memo 不起作用 - 我错过了什么?

我正在重构我们的一些组件,所以我正在尝试合并 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)

optimization memo reactjs react-hooks

5
推荐指数
1
解决办法
4195
查看次数

标签 统计

memo ×1

optimization ×1

react-hooks ×1

reactjs ×1