Last active
November 17, 2022 14:19
-
-
Save AXKuhta/2d56c7d2c3687351fb4beeebb8436c90 to your computer and use it in GitHub Desktop.
React fetch note
This file contains hidden or 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 "https://cdn.skypack.dev/[email protected]"; | |
import ReactDOM from "https://cdn.skypack.dev/[email protected]"; | |
function Demo ({inpv}) { | |
return (<pre><code>{inpv}</code></pre>); | |
} | |
function App () { | |
const [text, setText] = React.useState("Waiting..."); | |
const fetch_data = function() { | |
fetch(`https://jsonplaceholder.typicode.com/users`) | |
.then(response => response.json()) | |
.then(data => setText(JSON.stringify(data, null, 2))) | |
.catch(error => setText("Error")); | |
} | |
fetch_data() | |
return <Demo inpv={text}/>; | |
} | |
ReactDOM.render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment