Created
October 16, 2022 15:51
-
-
Save MonteLogic/c7703888a6673c77f90d3db47e877895 to your computer and use it in GitHub Desktop.
Fetch feature with useEffect.
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 "./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