Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created January 20, 2020 19:56
Show Gist options
  • Save bartcis/54a165e90e14af6926b3315c91aa50d2 to your computer and use it in GitHub Desktop.
Save bartcis/54a165e90e14af6926b3315c91aa50d2 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