Considering you want to silence react-hooks/exhaustive-deps eslint rule for specific names, I have written a patch for that rule, which allows me to use the following config:
'react-hooks/exhaustive-deps': [
'warn',
{
stableHooksPattern: 'useDispatch|useSharedValue|useErrorDropdown',
ignoredDepsPattern: '^navigation|navigate|popToTop|pop$',
},
],
which essentially results in the following hook call being "clean":
const navigation = useNavigation();
useEffect(() => {
navigation.navigate('Voila');
}, []);
Here's the source code of my patch for eslint-plugin-react-hooks@4.3.0:
diff --git a/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js b/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
index 29fb123..b22bc3f 100644
--- a/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
+++ b/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
@@ -735,6 +735,12 @@ var ExhaustiveDeps = {
},
enableDangerousAutofixThisMayCauseInfiniteLoops: {
type: 'boolean'
+ },
+ stableHooksPattern: {
+ type: 'string'
+ },
+ ignoredDepsPattern: {
+ type: 'string'
}
}
}]
@@ -743,9 +749,13 @@ var ExhaustiveDeps = {
// Parse the `additionalHooks` regex.
var additionalHooks = context.options && context.options[0] && context.options[0].additionalHooks ? new RegExp(context.options[0].additionalHooks) : undefined;
var enableDangerousAutofixThisMayCauseInfiniteLoops = context.options && context.options[0] && context.options[0].enableDangerousAutofixThisMayCauseInfiniteLoops || false;
+ var stableHooksPattern = context.options && context.options[0] && context.options[0].stableHooksPattern ? new RegExp(context.options[0].stableHooksPattern) : undefined;
+ var ignoredDepsPattern = context.options && context.options[0] && context.options[0].ignoredDepsPattern ? new RegExp(context.options[0].ignoredDepsPattern) : undefined;
var options = {
additionalHooks: additionalHooks,
- enableDangerousAutofixThisMayCauseInfiniteLoops: enableDangerousAutofixThisMayCauseInfiniteLoops
+ enableDangerousAutofixThisMayCauseInfiniteLoops: enableDangerousAutofixThisMayCauseInfiniteLoops,
+ stableHooksPattern: stableHooksPattern,
+ ignoredDepsPattern: ignoredDepsPattern
};
function reportProblem(problem) {
@@ -904,7 +914,9 @@ var ExhaustiveDeps = {
var _callee = callee,
name = _callee.name;
- if (name === 'useRef' && id.type === 'Identifier') {
+ if (options.stableHooksPattern && options.stableHooksPattern.test(name)) {
+ return true;
+ } else if (name === 'useRef' && id.type === 'Identifier') {
// useRef() return value is stable.
return true;
} else if (name === 'useState' || name === 'useReducer') {
@@ -1098,7 +1110,7 @@ var ExhaustiveDeps = {
if (!dependencies.has(dependency)) {
var resolved = reference.resolved;
- var isStable = memoizedIsStablecKnownHookValue(resolved) || memoizedIsFunctionWithoutCapturedValues(resolved);
+ var isStable = memoizedIsStablecKnownHookValue(resolved) || memoizedIsFunctionWithoutCapturedValues(resolved) || (options.ignoredDepsPattern && options.ignoredDepsPattern.test(resolved.name));
dependencies.set(dependency, {
isStable: isStable,
references: [reference]
The filename for the patch is eslint-plugin-react-hooks+4.3.0.patch, and you should place it into patches directory at the root of your repository.
Disclaimer: this will only work globally. So if you want to declare a return value as "stable", you will have to list it in the eslint config, and it will take effect globally in your project.
If you want to be able to "mark" return values as stable, you will need to further modify that eslint plugin to make it work. Unfortunately, I don't have time for that.