简体   繁体   English

我什么时候在 React 中使用 useCallback?

[英]When sould I use useCallback in React?

It's really difficult for me to decide whether to use useCallback or not.我真的很难决定是否使用 useCallback。 If there isn't an expensive function, should I just omit it?如果没有昂贵的 function,我应该省略它吗? (But I don't know whether a function is expensive or not...) (但我不知道 function 是否昂贵......)

Or when a component is re-rendered frequently, I could wrap every function in it by useCallback?或者当一个组件频繁重新渲染时,我可以通过 useCallback 将每个 function 包装在其中吗?

Any idea?任何想法?

You can consider following use case regarding using useCallback您可以考虑以下有关使用 useCallback 的用例

React's useCallback Hook can be used to optimize the rendering behavior of your React function components. React 的 useCallback Hook 可用于优化 React function 组件的渲染行为。

Usually useCallback is very helpful during passing callback props to child components.通常useCallback在将回调道具传递给子组件时非常有用。

Let's say if a child component that accepts a callback relies on a referential equality check to prevent unnecessary re-renders when its props change, then it is important that any callback props do not change between renders.假设如果一个接受回调的子组件依赖于引用相等性检查来防止在其 props 更改时发生不必要的重新渲染,那么重要的是任何回调 props 在渲染之间都不会发生变化。

To do this, the parent component can wrap the callback prop in useCallback and be guaranteed that it is passing the same function object down into the optimised child component.为此,父组件可以将回调属性包装在 useCallback 中,并保证它将相同的 function object 向下传递到优化的子组件中。

Let's say you have a component that renders a big list of items.假设您有一个呈现大量项目的组件。

import React from 'react';
import useSearch from './fetch-items';

function ListItem({ value, handleClick }) {
  const items = useSearch(value);

  const itemToElement = item => <div onClick={handleClick}>{item}</div>;

  return <div>{items.map(itemToElement)}</div>;
}

export default React.memo(ListItem);

Here, ListItem renders a list of items.在这里, ListItem呈现项目列表。 Let's imagine the list could be big, probably a few thousands of items.让我们想象一下这个列表可能很大,可能有几千个项目。 To preserve the list re-rendering, you wrap it into React.memo .要保留列表重新渲染,请将其包装到React.memo中。

The parent component of ListItem needs provides a handler function when an item is clicked. ListItem的父组件需要在单击项目时提供一个处理程序 function。

import React, { useCallback } from 'react';

export default function ParentComponent({ value }) {
  const handleClick = useCallback(item => {
    console.log('List item is clicked', item);
  }, [value]);

  return (
    <ListItem
      value={value}
      handleClick={handleClick}
    />
  );
}

handleClick callback is memoizied by useCallback() . handleClick回调由useCallback() 记忆 As long as term variable stays the same, useCallback() returns the same function instance.只要 term 变量保持不变, useCallback() 就会返回相同的 function 实例。

Even if for some reason ParentComponent component re-renders, handleClick stays the same and doesn't break the memoization of ListItem.即使由于某种原因ParentComponent组件重新渲染, handleClick保持不变并且不会破坏 ListItem 的记忆。

Notes: Please don't mix React's useCallback Hook with React's memo API.注意:请不要将 React 的 useCallback Hook 与 React 的备忘录 API 混用。 useCallback is used to memoize functions whereas React memo is used to wrap React components to prevent re-renderings. useCallback 用于 memoize 函数,而 React memo 用于包装 React 组件以防止重新渲染。 They provide two different functionality.它们提供两种不同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM