Created
March 7, 2020 08:40
-
-
Save DennyScott/38acd067a5ef3f97dec8f284a973a9d1 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
export function CatFacts({ id }) { | |
const [data, setData] = useState(); | |
const [urlOfData, setUrlOfData] = useState(); | |
const proxyUrl = "https://cors-anywhere.herokuapp.com/"; | |
const targetUrl = `https://cat-fact.herokuapp.com/facts/${id}`; | |
if (!data || urlOfData !== id) { | |
setTimeout(() => { | |
fetch(proxyUrl + targetUrl) | |
.then(response => response.json()) | |
.then(facts => { | |
setData(facts.text); | |
setUrlOfData(id); | |
}); | |
}, 5000); | |
} | |
return <div>Cat Fact: {data}</div>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment