📅  最后修改于: 2022-03-11 15:03:09.382000             🧑  作者: Mango
import React,{useState, createContext} from 'react';
export const ProductsContext = createContext();
export const ProductsProvider = props => {
const [categories, setCategories] = useState({ categories: {} });
const [products, setProducts] = useState({ products: {} });
const [loading, setLoading] = useState(true);
useEffect(() => {
(async () => {
const [categoriesResult, productsResult] = await Promise.all([fetch('http://localhost:3000/api/categories'), fetch('http://localhost:3000/api/products')]);
setCategories(categoriesResult);
setProducts(productsResult);
setLoading(false);
})();
}, []);
return (
{props.children}
);
}