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.
2. 解释
// 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;
-
如果 react + ts, useCallback 必须传入第二个参数,useMemo 可选填
-
如果传入的第二个参数是一个 空数组,后续的渲染,只会记住第一次渲染的
If no array is provided, a new value will be computed on every render.
如果 useMemo 不传入参数,相当于没有 使用 useMemo, 每次都会重新 computed
本文介绍了在React中,useCallback和useMemo函数的第二个参数为空数组与不传的区别。当参数为空数组时,返回值仅计算一次;若未提供数组,每次渲染都将重新计算,导致失去缓存效果。
796

被折叠的 条评论
为什么被折叠?



