Changeset cfc16a3 for my-react-app/src/components/Restaurants.js
- Timestamp:
- 03/03/24 10:52:49 (15 months ago)
- Branches:
- main
- Children:
- c63036a
- Parents:
- 75f5086
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
my-react-app/src/components/Restaurants.js
r75f5086 rcfc16a3 1 import React, { useState, useEffect } from 'react'; 2 import axios from 'axios'; 1 import React, {useState, useEffect, useContext} from 'react'; 3 2 import 'bootstrap/dist/css/bootstrap.min.css'; 4 3 import StarRating from './StarRating'; 5 4 import { useNavigate } from 'react-router-dom'; 5 import {RestaurantContext} from "./RestaurantContext"; 6 6 7 7 const Restaurants = () => { 8 8 const [restaurants, setRestaurants] = useState([]); 9 9 const navigate = useNavigate(); 10 const restaurantContext = useContext(RestaurantContext); 10 11 11 12 useEffect(() => { 12 const fetchRestaurants = async () => { 13 try { 14 const response = await axios.get('http://localhost:8080/api/restaurants'); 15 setRestaurants(response.data); 16 } catch (error) { 17 console.error('Error fetching restaurants:', error); 18 } 19 }; 20 21 fetchRestaurants(); 22 }, []); 13 setRestaurants(restaurantContext.restaurants); 14 }, [restaurantContext]); 23 15 24 16 const handleDetailClick = (restaurantId) => { … … 98 90 </div> 99 91 </div> 100 <button onClick={() => handleDetailClick(restaurant.restaurantId)} className="btn btn-primary">View Details</button> 92 <button onClick={() => handleDetailClick(restaurant.restaurantId)} 93 className="btn btn-primary">View Details 94 </button> 101 95 </div> 102 96 </div>
Note:
See TracChangeset
for help on using the changeset viewer.