Created
August 5, 2019 13:04
-
-
Save ynotdraw/95fe08e0585b43cfda014e2b50969242 to your computer and use it in GitHub Desktop.
Fetching data with hooks, example from: https://github.com/ryanlanciaux/coffee-shop-example/blob/master/src/screens/index.js
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, { useState, useContext, useEffect } from "react"; | |
import { getCoffeeData } from "../api/getData"; | |
import Layout from "../components/Layout"; | |
import Loading from "../components/Loading"; | |
import ItemGroup from "../components/ItemGroup"; | |
import Cart from "../components/Cart"; | |
import { GlobalContext } from "../components/GlobalStateProvider"; | |
// this should be a reducer | |
function useCoffeeData() { | |
const [data, setData] = useState(); | |
const [loading, setLoading] = useState(true); | |
const [error, setError] = useState(); | |
useEffect(() => { | |
const getData = async () => { | |
try { | |
const newData = await getCoffeeData(); | |
setData(newData); | |
setLoading(false); | |
} catch (ex) { | |
setData(undefined); | |
setLoading(false); | |
setError(ex); | |
} | |
}; | |
getData(); | |
}, []); | |
return { data, isLoading: loading, error }; | |
} | |
export default function() { | |
const { data, isLoading, error } = useCoffeeData(); | |
const [showCart, setShowCart] = useState(false); | |
const { dispatch } = useContext(GlobalContext); | |
return ( | |
<Layout onToggleCart={() => setShowCart(!showCart)} showCart={showCart}> | |
{isLoading && <Loading />} | |
{showCart && <Cart />} | |
{!showCart && data && ( | |
<ItemGroup | |
data={data} | |
addToCart={item => dispatch({ type: "ADD_ITEM", payload: item })} | |
/> | |
)} | |
{error && JSON.stringify(error)} | |
</Layout> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment