Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Created October 16, 2022 15:51
Show Gist options
  • Save MonteLogic/c7703888a6673c77f90d3db47e877895 to your computer and use it in GitHub Desktop.
Save MonteLogic/c7703888a6673c77f90d3db47e877895 to your computer and use it in GitHub Desktop.
Fetch feature with useEffect.
import "./styles.css";
import React, { useEffect, useState } from "react";
export default function App() {
const [resourceType, setResourceType] = useState("posts");
console.log("render");
useEffect(() => {
fetch(`https://jsonplaceholder.typicode.com/${resourceType}`)
.then((response) => response.json())
.then((json) => console.log(json));
});
return (
<div className="App">
<button onClick={() => setResourceType("posts")}>Posts</button>
<button onClick={() => setResourceType("users")}>Users</button>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment