source: my-react-app/src/components/RestaurantContext.js@ 2518b3a

main
Last change on this file since 2518b3a was 2518b3a, checked in by Aleksandar Panovski <apano77@…>, 2 weeks ago

Added menu tag

succesfull testing and implemnation

  • Property mode set to 100644
File size: 896 bytes
RevLine 
[cfc16a3]1import React, { createContext, useState, useEffect } from 'react';
2import axios from 'axios';
3
4export const RestaurantContext = createContext();
5
6export const RestaurantProvider = ({ children }) => {
7 const [restaurants, setRestaurants] = useState([]);
8
9 useEffect(() => {
[2518b3a]10 if (restaurants.length > 0) return;
[cfc16a3]11 const fetchRestaurants = async () => {
12 try {
[8ca35dc]13 const response = await axios.get('http://localhost:8081/api/restaurants');
[2518b3a]14 console.log("Fetched once", response.data);
[cfc16a3]15 setRestaurants(response.data);
16 } catch (error) {
17 console.error('Error fetching restaurants:', error);
18 }
19 };
[f5b256e]20 fetchRestaurants();
[cfc16a3]21 }, []);
22
23 return (
24 <RestaurantContext.Provider value={{ restaurants }}>
25 {children}
26 </RestaurantContext.Provider>
27 );
28};
Note: See TracBrowser for help on using the repository browser.