Skip to content

Instantly share code, notes, and snippets.

@DennyScott
Created March 7, 2020 08:40
Show Gist options
  • Save DennyScott/38acd067a5ef3f97dec8f284a973a9d1 to your computer and use it in GitHub Desktop.
Save DennyScott/38acd067a5ef3f97dec8f284a973a9d1 to your computer and use it in GitHub Desktop.
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