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

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

Fixed bugs and fully developed menu now. Readonly added.

  • Property mode set to 100644
File size: 836 bytes
Line 
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(() => {
10 if (restaurants.length > 0) return;
11 const fetchRestaurants = async () => {
12 try {
13 const response = await axios.get('http://localhost:8081/api/restaurants');
14 setRestaurants(response.data);
15 } catch (error) {
16 console.error('Error fetching restaurants:', error);
17 }
18 };
19 fetchRestaurants();
20 }, []);
21
22 return (
23 <RestaurantContext.Provider value={{ restaurants }}>
24 {children}
25 </RestaurantContext.Provider>
26 );
27};
Note: See TracBrowser for help on using the repository browser.