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
|
Line | |
---|
1 | import React, { createContext, useState, useEffect } from 'react';
|
---|
2 | import axios from 'axios';
|
---|
3 |
|
---|
4 | export const RestaurantContext = createContext();
|
---|
5 |
|
---|
6 | export 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 | console.log("Fetched once", response.data);
|
---|
15 | setRestaurants(response.data);
|
---|
16 | } catch (error) {
|
---|
17 | console.error('Error fetching restaurants:', error);
|
---|
18 | }
|
---|
19 | };
|
---|
20 | fetchRestaurants();
|
---|
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.