In React Typescript with the exhaustive-deps rule enabled, when I define a ref and use that inside of an effect, the linter is fine with it:
const stringRef: RefObject<string> = useRef("Hello World!");
useEffect(() => {
console.log(stringRef.current);
}, []) // no warning, the linter detects that I'm using a ref
However, when I put the effect inside of a custom hook, the linter complains that I should include the ref inside of the dependency array:
const stringRef: RefObject<string> = useRef("Hello World!");
useCustomHook(stringRef);
// in another-file.ts
const useCustomHook = (ref: RefObject<string>) => {
useEffect(() => {
console.log(ref.current);
}, []) // ESLint: React Hook useEffect has a missing dependency: 'ref'. Either include it or remove the dependency array.(react-hooks/exhaustive-deps)
}
Semantically, nothing changed, however, the linter doesn't recognize that ref is a RefObject (even though I typed it as such).
The big question now is: How can I make the linter know that the given dependency doesn't need to be included in the dependency array without suppressing the warning?
For me, it's a major drawback that this is not possible, because I can't convert my effects into custom hooks without the linter complaining.
Thanks for your help.