Skip to content

Instantly share code, notes, and snippets.

@jgthms
Forked from bartcis/fetch_hook.js
Created July 22, 2022 08:16
Show Gist options
  • Save jgthms/75347367af31c87ab743e5b8a9b4522f to your computer and use it in GitHub Desktop.
Save jgthms/75347367af31c87ab743e5b8a9b4522f to your computer and use it in GitHub Desktop.
Fetch from API as custom Hook
import {compile} from 'path-to-regexp';
import {GET_ARTICLE_PATH} from './articles-routes';
export const useGetSingleArticle = ({ articleId, abortController = new AbortController()}) => {
const baseUrl = 'https://jsonplaceholder.typicode.com';
const path = baseUrl + compile(GET_ARTICLE_PATH)({articleId});
const { signal, abort } = abortController || {};
const articleRequest = fetch(path, {
signal: signal,
method: 'GET',
});
return [articleRequest, abort?.bind(abortController)];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment