Created
April 9, 2020 14:21
-
-
Save NickFoden/4ea80173b3845d852603e99559eaf76a 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; |
Author
NickFoden
commented
Apr 9, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment