Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Created June 15, 2020 18:11
Show Gist options
  • Save manuel-mauky/a23ccf37fd3a186cac6b5c8b323f95e2 to your computer and use it in GitHub Desktop.
Save manuel-mauky/a23ccf37fd3a186cac6b5c8b323f95e2 to your computer and use it in GitHub Desktop.
React-Hooks-Article: Load data in function component with useState and useEffect
import React, { useEffect, useState } from "react"
export const LoadDataFunction = () => {
const [data, setData] = useState()
useEffect(() => {
fetch("/some-data.json")
.then((response) => response.json())
.then((data) => {
setData(data)
})
})
return (
<div>
{data.map((item) => (
<p>{item}</p>
))}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment