在列表中对React组件回调进行记忆化处理

huangapple go评论72阅读模式
英文:

memoize react component callback in a list

问题

以下是您要的代码部分的中文翻译:

const SomeComponent = ({ items }: { items: ItemData[] }) => {
  const getItemCallback = (item: ItemData) => (e: React.MouseEvent) => {
    e.preventDefault();

    // 使用 item 做一些操作
  }

  return <div>
    {items.map(item => {
      const callback = useCallback(getItemCallback(item));
    
      return <Item title={item.title} itemCallback={callback} />
    })}

  </div>
}

请注意,上述翻译仅包括代码部分,没有其他内容。

英文:

How can I memoize a callback generated in the map loop? This (obviously) gives an error:

const SomeComponent = ({ items }: { items: ItemData[] }) =&gt; {
  const getItemCallback = (item: ItemData) =&gt; (e: React.MouseEvent) =&gt; {
    e.preventDefault();

    //do something with item
  }

  return &lt;div&gt;
    {items.map(item =&gt; {
      const callback = useCallback(getItemCallback(item));
    
      return &lt;Item title={item.title} itemCallback={callback} /&gt;
    })}

  &lt;/div&gt;
}

在列表中对React组件回调进行记忆化处理

答案1

得分: 1

包装 getItemCallbackuseCallback 中,并将其传递给组件:

const SomeComponent = ({ items }: { items: ItemData[] }) => {
  const getItemCallback = useCallback((item: ItemData) => (e: React.MouseEvent) => {
    e.preventDefault();

    // 对项目执行一些操作
  }, []);

  return <div>
    {items.map(item => (
      <Item 
        key={item.key}
        item={item} 
        itemCallback={getItemCallback} 
        />
    )}
  </div>
}

然后,组件调用回调函数,并传递 item 来创建一个经过 useCallback 包装的新函数:

const Item = ({ item, itemCallback }) => {
  const callback = useCallback(itemCallback(item), []);
  
  return (
    // ...
  );
}
英文:

Wrap getItemCallback in useCallback and pass it to the component:

const SomeComponent = ({ items }: { items: ItemData[] }) =&gt; {
  const getItemCallback = useCallback((item: ItemData) =&gt; (e: React.MouseEvent) =&gt; {
    e.preventDefault();

    //do something with item
  }, []);

  return &lt;div&gt;
    {items.map(item =&gt; (
      &lt;Item 
        key={item.key}
        item={item} 
        itemCallback={getItemCallback} 
        /&gt;
    ))}
  &lt;/div&gt;
}

The component then calls the callback, and passes the item to create a new function wrapped with useCallback:

const Item = ({ item, itemCallback }) =&gt; {
  const callback = useCallback(itemCallback(item), []);
  
  return (
    ...
  );
}

答案2

得分: 0

const ItemList = ({ items }: { items: ItemData[] }) => {
  const getItemCallback = useCallback((item: ItemData) => (e: React.MouseEvent) => {
    e.preventDefault();

    //do something with item
  }, []);

  return (
    <div>
      {items.map(item => (
        <Item title={item.title} itemCallback={getItemCallback(item)} />
      ))}
    </div>
  );
};

const SomeComponent = ({ items }: { items: ItemData[] }) => {
  return <ItemList items={items} />;
};
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const ItemList = ({ items }: { items: ItemData[] }) =&gt; {
  const getItemCallback = useCallback((item: ItemData) =&gt; (e: React.MouseEvent) =&gt; {
    e.preventDefault();

    //do something with item
  }, []);

  return (
    &lt;div&gt;
      {items.map(item =&gt; (
        &lt;Item title={item.title} itemCallback={getItemCallback(item)} /&gt;
      ))}
    &lt;/div&gt;
  );
};

const SomeComponent = ({ items }: { items: ItemData[] }) =&gt; {
  return &lt;ItemList items={items} /&gt;;
};

<!-- end snippet -->

答案3

得分: 0

以下是您的代码的翻译部分:

const SomeComponent = ({ items }: { items: ItemData[] }) => {
  const getItemCallback = (item: ItemData) => (e: React.MouseEvent) => {
    e.preventDefault();

    //对项目执行某些操作
  }

  return (
    <div>
      {items.map(item => {
        const callback = useMemo(() => getItemCallback(item), [item]);
        return <Item title={item.title} itemCallback={callback} />;
      })}
    </div>
  );
};
英文:

The a updated version of your code using

const SomeComponent = ({ items }: { items: ItemData[] }) =&gt; {
      const getItemCallback = (item: ItemData) =&gt; (e: React.MouseEvent) =&gt; {
        e.preventDefault();
    
        //do something with item
      }
    
      return (
        &lt;div&gt;
          {items.map(item =&gt; {
            const callback = useMemo(() =&gt; getItemCallback(item), [item]);
            return &lt;Item title={item.title} itemCallback={callback} /&gt;;
          })}
        &lt;/div&gt;
      );
    };

huangapple
  • 本文由 发表于 2023年2月27日 15:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定