fix: cancels existing fetches if new fetches are started

This commit is contained in:
James
2023-03-13 11:22:10 -04:00
parent 657aa65e99
commit ccc92fdb75
2 changed files with 8 additions and 1 deletions

View File

@@ -12,7 +12,7 @@ export const requests = {
}
return fetch(`${url}${query}`, {
credentials: 'include',
headers: options.headers,
...options,
});
},

View File

@@ -43,12 +43,15 @@ const usePayloadAPI: UsePayloadAPI = (url, options = {}) => {
});
useEffect(() => {
const abortController = new AbortController();
const fetchData = async () => {
setIsError(false);
setIsLoading(true);
try {
const response = await requests.get(`${url}${search}`, {
signal: abortController.signal,
headers: {
'Accept-Language': i18n.language,
},
@@ -73,6 +76,10 @@ const usePayloadAPI: UsePayloadAPI = (url, options = {}) => {
setIsError(false);
setIsLoading(false);
}
return () => {
abortController.abort();
};
}, [url, locale, search, i18n.language]);
return [{ data, isLoading, isError }, { setParams }];