Created
April 9, 2020 14:21
-
-
Save NickFoden/5f0e9430de7b1079ef2a7ca69088f72e to your computer and use it in GitHub Desktop.
useEffect async promise
This file contains hidden or 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, { useState, useEffect } from "react"; | |
import { myPromise } from "../api"; | |
const index = () => { | |
const [data, setData] = useState([]); | |
useEffect(async () => { | |
const result = await myPromise(); | |
setData(result); | |
}, []); | |
return ( | |
<div> | |
<ul> | |
{data.map((item) => ( | |
{/* assumes each object in array has key property */} | |
<li key={item.key}>{item}</li> | |
))} | |
</ul> | |
</div> | |
); | |
}; | |
export default index; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment