-
-
Save jgthms/75347367af31c87ab743e5b8a9b4522f to your computer and use it in GitHub Desktop.
Fetch from API as custom Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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