I'm new to React hooks, but I'm trying to use a useEffect with a useCallback, but getting the notorious React Hook "useList" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks error.
This file holds the makeRequest:
function useConnections = () => {
const makeRequest = React.useCallback(async (props) => {
// Determine base url, determine headers here
const response = fetch(url, options);
return response;
}
return { makeRequest };
}
This file is my useListProvider:
function useListProvider = () => {
const { makeRequest } = useConnections();
const useList = React.useCallback(async (props) => {
// makerequest is just a wrapper for fetch with a bunch of needed headers.
const response = await makeRequest(props);
return { body: response.body };
}
return { useList };
}
This is the rendered page:
function MainPage() {
const [isBusy, setIsBusy] = React.useStore(false);
const { useList } = useListProvider();
React.useEffect(() => {
if (!isBusy) { useList(); setIsBusy(false); } // ERROR HERE!
}, [useList]);
return (
<React.Fragment>
IsBusy: {isBusy}
</React.Fragment>
);
}
Maybe I'm not getting it, but I only want to grab the useList data when the state says it's not busy. However, doing it this way, I get the error listed above. I understand I can't think of this the same way as Component classes, but how would you approach single and multiple renders from a callback?
I'm not entirely sure what is happening here because I'm doing something similar in the useConnections, etc. and not getting the same error?