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 warn me in the first example?
Can I safely ignore it in such circumstances?
field.setValue
is 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 passingfield
is just plain incorrect. – Nail