Skip to content

Instantly share code, notes, and snippets.

@Manntrix
Created February 2, 2023 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manntrix/2d25d793d3f627e579ac418387b0701c to your computer and use it in GitHub Desktop.
Save Manntrix/2d25d793d3f627e579ac418387b0701c to your computer and use it in GitHub Desktop.
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