[英]react-hooks/exhaustive-deps warning with custom IntersectionObserver hook
[英]Can I have react-hooks/exhaustive-deps for a custom hook?
我寫了這個鈎子(可能有錯誤,我還沒有使用它):
import { useCallback, useEffect } from 'react';
import _throttle from 'lodash/throttle';
export function useThrottledCallback(cb, delay, ...deps) {
const callback = useCallback(_throttle(cb, delay), deps);
useEffect(() => {
const lastCallback = callback;
return () => lastCallback.cancel();
}, [callback]);
return callback;
}
有沒有辦法讓這個鈎子的 exhaustive-deps rule lint 用法?
useThrottledCallback(() => (a + b + c)}, 100, [])
在此用法中,我希望收到通知a
、 b
和c
需要位於依賴項數組中。
這應該很容易。 文件說:
exhaustive-deps
可以配置為使用 additionalHooks 選項驗證自定義 Hooks 的依賴性。 此選項接受一個正則表達式來匹配具有依賴性的自定義掛鈎的名稱。
所以你想要這樣的東西:
{
"rules": {
// ...
"react-hooks/exhaustive-deps": ["warn", {
"additionalHooks": "useThrottledCallback"
}]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.