I'm trying to use React hooks for memoizing a callback. This callback specifically uses a function that's defined on an object.
const setValue = useCallback((value) => {
field.setValue(key, value);
}, [field.setValue, key]);
This triggers eslint rule react-hooks/exhaustive-deps. It wants me to instead pass in [field, key].
If I then change the code to the following, I get no warning even though it seems equivalent:
const { setValue: setFieldValue } = field;
const setValue = useCallback((value) => {
setFieldValue(key, value);
}, [setFieldValue, key]);
Why is eslint warning me in the first example? Can I safely ignore it in such circumstances?
field.setValueis certainly the better thing to pass. If react does deep object equality checking in hooks then this prevents un-necessary checks, and if they don't then passingfieldis just plain incorrect.