Skip to content

Instantly share code, notes, and snippets.

@NickFoden
Created April 9, 2020 14:21
Show Gist options
  • Save NickFoden/5f0e9430de7b1079ef2a7ca69088f72e to your computer and use it in GitHub Desktop.
Save NickFoden/5f0e9430de7b1079ef2a7ca69088f72e to your computer and use it in GitHub Desktop.
useEffect async promise
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