Advertisement
Guest User

Untitled

a guest
Dec 29th, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useState, useEffect} from 'react';
  2. import axios from "axios";
  3. import Button from 'react-bootstrap/Button';
  4. import Card from 'react-bootstrap/Card';
  5.  
  6. const Products = () => {
  7.  
  8. const [data, setData] = useState([]);
  9.  
  10. useEffect( () => {
  11.  
  12.     async function fetchData() {
  13.         try{
  14.             const response = await axios('http://localhost/JSON-PHP-API/');
  15.            
  16.              setData(...data, response.data);        
  17.            
  18.         } catch (err){
  19.  
  20.             console.log(err);
  21.  
  22.         }
  23.     }
  24.  
  25.     fetchData();
  26.     console.log(data);
  27.    
  28. }, [data]);
  29.  
  30.  
  31.  
  32. return(
  33.     <div className='container'>
  34.         {Object.entries(data).map(([key, product], i) => (
  35.             <Card style={{ width: '18rem' }}>
  36.             <Card.Img variant="top" src={require(`/public/pics/${product.picUrl}`)} />
  37.             <Card.Body>
  38.             <Card.Title> {product.name} </Card.Title>
  39.             <Card.Text> {product.beschreibung} </Card.Text>
  40.             <Button variant="primary">In den Warenkorb</Button>
  41.             </Card.Body>
  42.             </Card>
  43.         ))}
  44.     </div>
  45.  
  46. )
  47. }
  48.  
  49. export default Products;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement