Skip to content

Instantly share code, notes, and snippets.

@AXKuhta
Last active November 17, 2022 14:19
Show Gist options
  • Save AXKuhta/2d56c7d2c3687351fb4beeebb8436c90 to your computer and use it in GitHub Desktop.
Save AXKuhta/2d56c7d2c3687351fb4beeebb8436c90 to your computer and use it in GitHub Desktop.
React fetch note
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