The lint rule for hooks assumes that things which start with "use" are hooks. Thus it thinks that useList is a hook and trying to enforce the rules of hooks on it. But it's not actually a hook, it's just a normal function, so you just need to give it a different name and the lint rule will be satisfied.
function useListProvider = () => {
const { makeRequest } = useConnections();
const callback = React.useCallback(async (props) => {
const response = await makeRequest(props);
return { body: response.body };
}, [makeRequest])
return { callback };
}
// elsewhere:
const { callback } = useListProvider();
React.useEffect(() => {
if (!isBusy) {
callback();
setIsBusy(false);
}
}, [callback]);
Why is it not a hook? Well, a hook is either one of the built in hooks, or a function that calls one of the built in hooks. Your callback doesn't meet those criteria. It was created by useCallback, but that just means it's a memoized function, not a hook.
Answer from Nicholas Tower on Stack OverflowI have an async function that needs to be called when some state values evaluate to true. Is there any large difference in defining the async function in the use effect and calling it as opposed to defining it in a useCallback and then just calling the return from the useCallback in a useEffect?
// defined in useEffect
useEffect(() => {
const asyncFunc = asyc () => { // do something};
asyncFunc();
}, [dependencyArray]);vs
// defined in useCallback
const callBackFunc = useCallback(async () => {
// do something
}, [dependencyArra]);
useEffect(() => {
callBackFunc();
}, [callBackFunc]);