iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
自我挑戰組

從零開始學習React 系列 第 24

Day24 Create Image Gallery in React

  • 分享至 

  • xImage
  •  

在React中用bootstrap card建立圖片庫,並用Array.filter功能,點選上面的分類Button可選出不同類型的餐點。將Menu.js中資料透過useState()和Array.map()將資料一筆筆render出來。

https://ithelp.ithome.com.tw/upload/images/20211009/20139800RFO7B8kOGk.png

重要提醒:menu.js中的圖片要放在public/images中

記得要在App.js 中導入

import "../node_modules/bootstrap/dist/css/bootstrap.min.css"

App.js

import React from 'react';
import Fullcard from './Components/TabMenu/Fullcard';
import "../node_modules/bootstrap/dist/css/bootstrap.min.css"

const App = () => {
  return (
    <div>
      <Fullcard />
    </div>
  )
}

export default App

Fullcard.js

import React, { useState } from 'react';
import Menu from "./Menu";

const Fullcard = () => {
    const[items,setItems] = useState( Menu);

    const filterItem =(categItem) =>{
        const updatedItems = Menu.filter((curElem)=>{
            return  curElem.category === categItem ;
        });

        setItems(updatedItems);
    }

    return (
        <div>
            <h1 className="mt-5 text-center main-heading">Order Your Favourite Meal</h1>
            <hr />
            <div className="menu-tabs container">
               <div className="menu-tab d-flex justify-content-around">
                   <button className="btn btn-warning btn-lg" onClick={()=>filterItem('breakfast')}>Breakfast</button>
                   <button className="btn btn-warning btn-lg" onClick={()=>filterItem('lunch')}>Lunch</button>                   
                   <button className="btn btn-warning btn-lg" onClick={()=>filterItem('dinner')}>Dinner</button>
                   <button className="btn btn-warning btn-lg" onClick={()=>setItems(Menu)}>All Meal</button>
               </div>
            </div>
           
             <div className="container ">
                 <div className="row ">
                   <div className="col ">
                     <div className="row my-5 p-5 ">

                       {
                          items.map((elem) => {
                              const {id,name,image,description,price} = elem;

                              return(
                                
                                <div className="card my-5 p-5  border border-warning rounded " style={{width: '18rem'}}>
                                  <img src={image} class="card-img-top " ale={name} />
                                  <div className="card-body">
                                     <h5 className="card-title">{name}</h5>
                                     <p className="card-text">{description}</p>
                                     <p>Price:{price}</p>
                                    <a href="#" class="btn btn-warning">Order Now</a>
                                  </div>
                               </div>                           
                                                                         
                                    )
                                })
                             }
                         
                     </div>

                   </div>

                 </div>

             </div>

        </div>
    )
}

export default Fullcard

Menu.js

const Menu = [
    {
      id : 1,
      image : "image/break01.jpg",
      name : "Breakfast",
      category:"breakfast",
      price:"NT$199",
      description:"upto 90% off",
    } ,
    {
      id : 2,
      image : "image/dinner01.jpg",
      name : "Dinner",
      category:"dinner",
      price:"NT$399",
      description:"upto 90% off",
    } ,
    {
      id : 3,
      image : "image/break02.jpg",
      name : "Breakfast",
      category:"breakfast",
      price:"NT$199",
      description:"upto 90% off",
    } ,
    {
      id : 4,
      image : "image/lunch01.jpg",
      name : "Lunch",
      category:"lunch",
      price:"NT$299",
      description:"upto 90% off",
    } ,
    {
      id : 5,
      image : "image/dinner02.jpg",
      name : "Dinner",
      category:"dinner",
      price:"NT$399",
      description:"upto 90% off",
    } ,
    {
        id : 6,
        image : "image/break03.jpg",
        name : "Breakfast",
        category:"breakfast",
        price:"NT$199",
        description:"upto 90% off",
      } ,
      {
        id : 7,
        image : "image/lunch02.jpg",
        name : "Lunch",
        category:"lunch",
        price:"NT$299",
        description:"upto 90% off",
      } ,
      {
        id : 8,
        image : "image/lunch03.jpg",
        name : "Lunch",
        category:"lunch",
        price:"NT$299",
        description:"upto 90% off",
      } ,
          
    
  ]
  export default Menu;

上一篇
Day23 create React Navigation
下一篇
Day25 React Router useParams
系列文
從零開始學習React 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言