Created
February 2, 2023 18:59
-
-
Save Manntrix/2d25d793d3f627e579ac418387b0701c to your computer and use it in GitHub Desktop.
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 React, { useState, useEffect } from "react"; | |
const App = () => { | |
const [posts, setPosts] = useState([]); | |
const getData = () => { | |
var requestOptions = { | |
method: "GET", | |
redirect: "follow", | |
}; | |
fetch("http://localhost:3030/posts", requestOptions) | |
.then((response) => response.json()) | |
.then((result) => setPosts(result)) | |
.catch((error) => console.log("error", error)); | |
}; | |
useEffect(() => { | |
getData(); | |
}, []); | |
return ( | |
<div> | |
{posts.map((post) => ( | |
<div key={post.id}> | |
<h3> | |
<span>{post.id}</span> {post.title} | |
</h3> | |
<p>{post.body}</p> | |
</div> | |
))} | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment