Created
June 15, 2020 18:11
-
-
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
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, { 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