Skip to content

Instantly share code, notes, and snippets.

@heyMP
Created September 23, 2022 21:32
Show Gist options
  • Save heyMP/9dc589f3c7700097e7259881a9f03e82 to your computer and use it in GitHub Desktop.
Save heyMP/9dc589f3c7700097e7259881a9f03e82 to your computer and use it in GitHub Desktop.
React Fetch Data
import { useEffect } from "react";
const App = ({ placeholder, entry }) => {
const [categories, setCategories] = useState([]);
// useEffect(() => {
// fetch('https://google.com')
// .then(res => res.json())
// .then(res => {
// setCategories(res.data.categories)
// })
// .catch(err => console.log(error))
// }, []);
useEffect(async () => {
const res = await fetch('https://google.com').then(res => res.json())
setCategories(res.data.categories)
}, []);
return (
<>
<ul>
{categories.map(category => (
<li>{category}</li>
))}
</ul>
</>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment