useCallback 和 useMemo 与 redux-toolkit 的性能提升吗?

发布于 2025-01-11 02:58:27 字数 1118 浏览 0 评论 0原文

我已经看到很多关于为什么不应该使用 React 的 useCallback 和空依赖列表(这符合逻辑)的描述,但我一直在试图弄清楚是否有性能提升当它与 redux-toolkit 一起使用时,当依赖项列表包含 RT 对象时。 (该应用程序对于一个用户来说已经具有足够的性能,没有明显的差异,因此我希望在总体上做“正确的事情”。)

示例:

export const HomeSettings = () => {
  // redux-toolkit
  const { data, error, isLoading } = useGetUserQuery();
  const [updateUser] = useUpdateUserMutation();

  //useCallback providing an actual optimization here?
  const changeDataFilterOption = useCallback(
    (option: string) => {
      const someObj = someFunc(option, data);
      updateUser(someObj);
    },
    [data, updateUser]
  );

  //useMemo optimizing anything here?
  const itemList = useMemo(() => getListOfThings(data), [data]);

  if (isLoading) {
    return <LoadingMessage />;
  }
  if (error) {
    return <ErrorLoadingDataMessage />;
  }

  return (
    <div onClick={changeDataFilterOption}>
      onClick is silly here, just illustrating the point
      <div>{itemList}</div>
   </div>
  );
}

以这种方式使用 redux-toolkit ,记忆我的回调和变量有什​​么好处吗?

I've seen a lot of descriptions of why you shouldn't use React's useCallback with an empty dependency list (which makes logical sense), but I'm stuck trying to figure out if there are performance gains when using it alongside redux-toolkit, when the dependency list includes RT objects. (The application is already performant enough with one user that there's no perceptible difference, so I'm looking to do the "right thing" in the aggregate.)

An example:

export const HomeSettings = () => {
  // redux-toolkit
  const { data, error, isLoading } = useGetUserQuery();
  const [updateUser] = useUpdateUserMutation();

  //useCallback providing an actual optimization here?
  const changeDataFilterOption = useCallback(
    (option: string) => {
      const someObj = someFunc(option, data);
      updateUser(someObj);
    },
    [data, updateUser]
  );

  //useMemo optimizing anything here?
  const itemList = useMemo(() => getListOfThings(data), [data]);

  if (isLoading) {
    return <LoadingMessage />;
  }
  if (error) {
    return <ErrorLoadingDataMessage />;
  }

  return (
    <div onClick={changeDataFilterOption}>
      onClick is silly here, just illustrating the point
      <div>{itemList}</div>
   </div>
  );
}

Using redux-toolkit in this way, is there any benefit to memo-izing my callbacks and variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2025-01-18 02:58:27

以这种方式使用 Redux-Toolkit,对我的记忆化有什么好处吗?
回调和变量?

  • 使用useCallback来记忆changeDataFilterOption函数

    记住 changeDataFilterOption 函数没有任何好处,因为它不会作为 props 传递给另一个 React 组件,而在另一个 React 组件中,它的引用稳定性会成为问题。它作为 div 元素的 onClick 处理程序传递。

  • 使用 useMemo 记住 itemList

    仅当记住 getListOfThings(data)结果可能有好处 >数据参考更新。如果 getListOfThings 是一个繁重/昂贵的函数调用,则可以通过重新计算值来保存以前的结果,因为输入没有改变,我们知道这些值将是相同的。这显然假设 useGetUserQuery 正在提供稳定的获取的数据引用,我相信它确实如此,仅在端点运行时更新并更新查询缓存 .


一般经验法则

  1. useCallback 的一个良好的一般经验法则是,如果将回调函数作为 props 传递给其他 React 组件,您需要记住并提供稳定的回调函数。

  2. useMemo 的一个良好的一般经验法则是仅在以下情况下记忆值

    • 它们作为 props 传递给其他 React 组件,因此它们作为稳定的引用提供。
    • 计算成本“昂贵”。

Using Redux-Toolkit in this way, is there any benefit to memo-izing my
callbacks and variables?

  • Using useCallback to memoize the changeDataFilterOption function

    There is no benefit to memoizing the changeDataFilterOption function since it is not passed down as a prop to another React component where its reference stability would be an issue. It is passed as a div element's onClick handler.

  • Using useMemo to memoize the itemList value

    There is a likely benefit here to memoizing the result of getListOfThings(data) only when the data reference updates. If getListOfThings was a heavy/costly function call memoizing previous results saves from recomputing values that we know would be the same since the input didn't change. This obviously assumes the useGetUserQuery is providing a stable fetched data reference, which I believe it does, only updating when endpoint runs and updates the query cache.

General Rules of Thumb

  1. A good general rule of thumb for useCallback is that you'll want to memoize and provide stable callback functions if passing them down as props to other React components.

  2. A good general rule of thumb for useMemo is to only memoize values if

    • They are passed down as props to other React components so they are provided as stable references.
    • Computationally "expensive" to compute.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文