main
Rev | Line | |
---|
[cfc16a3] | 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 | const fetchRestaurants = async () => {
|
---|
| 11 | try {
|
---|
| 12 | const response = await axios.get('http://localhost:8080/api/restaurants');
|
---|
| 13 | setRestaurants(response.data);
|
---|
| 14 | } catch (error) {
|
---|
| 15 | console.error('Error fetching restaurants:', error);
|
---|
| 16 | }
|
---|
| 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.