usecontext from localhost

Solutions on MaxInterview for usecontext from localhost by the best coders in the world

showing results for - "usecontext from localhost"
Nour
21 Feb 2018
1import React,{useState, createContext} from 'react';
2
3export const ProductsContext = createContext();
4
5export const ProductsProvider = props => {
6    const [categories, setCategories] = useState({ categories: {} });
7    const [products, setProducts] = useState({ products: {} });
8    const [loading, setLoading] = useState(true);
9    useEffect(() => {
10        (async () => {
11            const [categoriesResult, productsResult] = await Promise.all([fetch('http://localhost:3000/api/categories'), fetch('http://localhost:3000/api/products')]);
12            setCategories(categoriesResult);
13            setProducts(productsResult);
14            setLoading(false);
15          })();
16        }, []);
17    return (
18    <ProductsContext.Provider value={ { products, categories, loading } }>
19        {props.children}
20    </ProductsContext.Provider>
21    );
22}
23
similar questions
queries leading to this page
usecontext from localhost