我是靠谱客的博主 迷人康乃馨,这篇文章主要介绍useCallback Vs useMemo 中,第二个参数传入 空数组 和 不传 的区别,现在分享给大家,希望可以做个参考。

1. 引用

For both useMemo and useCallback (which is essentially just a special case of useMemo), if the second argument is an empty array, the value will be memoized once and always returned.

If the second argument is omitted, the value will never be memoized, and the useCallback and the useMemo doesn’t do anything.

statck overflow

2. 解释

复制代码
1
2
3
4
5
// Require a second argument, and it must be an array function useCallback<T extends (...args: any[]) => any>(callback: T, deps: DependencyList): T; // Second argument can be undefined, but must be explicitly passed as undefined, not omitted. function useMemo<T>(factory: () => T, deps: DependencyList | undefined): T;
  1. 如果 react + ts, useCallback 必须传入第二个参数,useMemo 可选填

  2. 如果传入的第二个参数是一个 空数组,后续的渲染,只会记住第一次渲染的

If no array is provided, a new value will be computed on every render.

react 官网

如果 useMemo 不传入参数,相当于没有 使用 useMemo, 每次都会重新 computed

最后

以上就是迷人康乃馨最近收集整理的关于useCallback Vs useMemo 中,第二个参数传入 空数组 和 不传 的区别的全部内容,更多相关useCallback内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部