Skip to content

Instantly share code, notes, and snippets.

@hannahpun
Created January 6, 2022 06:17
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 hannahpun/a095532d0454b56096896f1a6c7a1ba3 to your computer and use it in GitHub Desktop.
Save hannahpun/a095532d0454b56096896f1a6c7a1ba3 to your computer and use it in GitHub Desktop.
const [lists, setLists] = useState([])
useEffect(() => {
getPosts()
},[])
// promise
const getPosts = () => {
fetch(`https://jsonplaceholder.typicode.com/posts`)
.then( res => res.json())
.then(json => {
setLists(json)
})
}
// asyn await
const getPosts = async () => {
const obj = await fetch(`https://jsonplaceholder.typicode.com/posts`)
const json = await obj.json()
setLists(json)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment