Created
January 20, 2020 19:56
-
-
Save bartcis/54a165e90e14af6926b3315c91aa50d2 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