Created
June 5, 2020 15:29
-
-
Save 3nvi/86366e74fc90d64d5a9b43d8891b5537 to your computer and use it in GitHub Desktop.
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 React from 'react'; | |
import { useHistory } from 'react-router-dom'; | |
const useQuery = ({ url }) => { | |
const history = useHistory(); | |
const [apiData, setApiData] = React.useState(); | |
React.useEffect(() => { | |
fetch(url) | |
.then(data => data.json()) | |
.then(({ code, status, ...apiData }) => { | |
if (code > 400) { | |
history.replace(history.location.pathname, { | |
errorStatusCode: code | |
}); | |
} else { | |
setApiData(apiData); | |
} | |
}); | |
}, [url]); | |
return { data: apiData } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment