0

For example, consider the following code :

const useCustomRef = initialValue => useRef(initialValue);

const CustomComponent = () => {
  const ref1 = useRef();
  const ref2 = useCustomRef();

  useEffect(() => {
    ref2.current.otherRef = ref1.current;
  }, []);
  
  return <div>Ref test</div>;
};

(Note: Nevermind the silliness, here, this is only to make point.)

Now, importing (and rendering) this dummy component in an app running with react-scripts will print in the console :

path/to/dummy/component.jsx
Line 70:7:  React Hook useEffect has a missing dependency: 'ref2'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

Is that warning built-in, or is it possible to specify a hooks' immutable properties?

For example, certain hooks return immutable and immutable properties, like :

const ref = useRef();                              // immutable
const handleCallback = useCallback(() => {}, []);  // mutable
const [ value, setValue ] = useState();            // mutable, immutable
const [ state, dispatch ] = useReducer(...);       // mutable, immutable
...

So, can a user-defined hook define the same properties so that they are not required as dependencies? I find it annoying to implement custom hooks returning immutable properties that are, then, required as dependencies while they would not otherwise (like for ref2 in the example above).

5
  • Would npmjs.com/package/… help? Commented Oct 26, 2021 at 14:47
  • @AKX if I'm not mistaken, this only allows the Linter to check (i.e. validate) for dependencies, it does not solve the problem described, here. Commented Oct 26, 2021 at 15:15
  • Ah yeah, I'm sorry, I just skimmed a little too hard there. Commented Oct 26, 2021 at 15:58
  • It looks like there's no extension point for that in eslint-plugin-react-hooks, it just has "tribal knowledge" about which values are guaranteed stable... Commented Oct 26, 2021 at 16:01
  • @AKX interesting. The code is actually clean enough to support custom hooks. Good catch! Commented Oct 26, 2021 at 19:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.