Skip to content

Instantly share code, notes, and snippets.

@jwchang0206
Created January 4, 2021 06:12
Show Gist options
  • Save jwchang0206/7385b69a43ce5ddbb20fd7be001b6fa4 to your computer and use it in GitHub Desktop.
Save jwchang0206/7385b69a43ce5ddbb20fd7be001b6fa4 to your computer and use it in GitHub Desktop.
import { useCallback, useEffect } from 'react';
import { useCancelTokens } from '../util/xhrHooks';
import { getItems as getItemsAction } from '.';
export const useItems = () => {
const addCancelToken, cancelAll } = useCancelTokens;
const getItems = useCallback(async () => {
// Cancel existing getItems requests before call a new one
cancelAll();
// Get a new token for a new request
const cancelToken = addCancelToken();
// Pass the cancel token for the request
return await getItemsAction({ cancelToken });
}, [addCancelToken, cancelAll]);
return { getItems };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment