Created
February 5, 2021 02:26
-
-
Save carmandomx/6d2aebfeee2a58237d19c9abbe465961 to your computer and use it in GitHub Desktop.
Ejemplo simple de como llamar un API en React
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 "./App.css"; | |
import React, { useState, useEffect } from "react"; | |
const getQuotesFromGit = () => { | |
const res = fetch( | |
"https://gist.githubusercontent.com/carmandomx/3d7ac5f15af87a587e1d25f5ba96de61/raw/e2d848b87c730a580077de221666343c15b1a243/gistfile1.txt", | |
{ mode: "cors" } | |
); | |
return res.then((value) => value.json()); | |
}; | |
function App() { | |
const [counter, setCounter] = useState(0); | |
const effectCallback = () => { | |
getQuotesFromGit().then((data) => { | |
console.log(data); | |
}); | |
}; | |
useEffect(effectCallback, []); | |
return ( | |
<div className="App"> | |
<h1>{counter}</h1> | |
<button | |
onClick={() => { | |
setCounter((prev) => prev + 1); | |
// console.log(counter); | |
}} | |
> | |
Aumenta | |
</button> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment