Issue: I am trying to fetch the data from a specific button component through onClick but it seems that I am quite confused on how to exactly do it. The button component which was rendered should have the functionality of displaying its specific data on a page when the user clicks on it.
Goal: If a specific menu item button(consisting the menu name, price, and number of available) is clicked, then it should display its menu name, price, and number of available inside the current page or ideally, it can also display on a different page.
My source code:
import * as React from "react";
import { useState } from "react";
import { Stack } from "@mui/material";
import ButtonCategoryStyle from "./ButtonCategoryStyle";
import BBQButtons from "./categoryButtons/BBQButtons";
import BilaoButtons from "./categoryButtons/BilaoButtons";
import ChickenButtons from "./categoryButtons/ChickenButtons";
export default function HomeOrderPage() {
const categories = ["BBQ", "Bilao", "Chicken"];
const [myCategory, setMyCategory] = useState("");
return (
<div>
{/* RENDERS THE MENU ITEM BUTTONS: */}
<Stack spacing={0} direction="row">
{categories.map((category) => (
<ButtonCategoryStyle
title={category.toLocaleUpperCase()}
key={category}
onClick={() => setMyCategory(category)}
/>
))}
</Stack>
<div>
<p>
{myCategory === "BBQ" && <BBQButtons />}
{myCategory === "Bilao" && <BilaoButtons />}
{myCategory === "Chicken" && <ChickenButtons />}
</p>
</div>
</div>
);
}
Specific button component (BBQ):
import * as React from "react";
import { Stack } from "@mui/material";
import { Button } from "@mui/material";
import Typography from "@mui/material/Typography";
import ButtonCategoryStyle from "../ButtonCategoryStyle";
import ItemsCategoryButton from "../ItemsCategoryButton";
function preventDefault(event) {
event.preventDefault();
}
export default function BBQButtons() {
return (
<React.Fragment>
<Stack spacing={0} direction="row" sx={{ mb: 4.5 }}>
<ItemsCategoryButton
title="Hito (Small)"
price="120.00"
availables="20"
-- onClick here????? --
/>
<ItemsCategoryButton
title="Hito (Medium)"
price="160.00"
availables="20"
/>
<ItemsCategoryButton
title="Hito (Large)"
price="210.00"
availables="20"
/>
<ItemsCategoryButton
title="Chicken Paa"
price="75.00"
availables="20"
/>
<ItemsCategoryButton
title="Chicken Pecho"
price="85.00"
availables="20"
/>
</Stack>
<Stack spacing={0} direction="row" sx={{ mb: 4.5 }}>
<ItemsCategoryButton
title="Chicken Baticulon"
price="15.00"
availables="20"
/>
<ItemsCategoryButton
title="Chicken Isaw (5 sticks)"
price="25.00"
availables="20"
/>
<ItemsCategoryButton
title="Chicken Atay"
price="15.00"
availables="20"
/>
<ItemsCategoryButton
title="Pork Tocino"
price="10.00"
availables="20"
/>
<ItemsCategoryButton
title="Pork Maskara"
price="25.00"
availables="20"
/>
</Stack>
<Stack spacing={0} direction="row" sx={{ mb: 4.5 }}>
<ItemsCategoryButton
title="Pork Belly"
price="140.00"
availables="20"
/>
<ItemsCategoryButton
title="Hotdog (Beefies)"
price="15.00"
availables="20"
/>
</Stack>
</React.Fragment>
);
}
Full source code in Codesandbox: https://codesandbox.io/s/vibrant-germain-76p1er?file=/src/HomeOrderPage.jsx:0-1069
(Either BBQ, BILAO, or CHICKEN button should be clicked first in order to display the menu items.)
Old StackOverflow questions I read which still confuses me:
- React read value of button clicked
- react fetch data on button click
- Fetch data on Button click in React (not StackOverflow)
Should I use Axios for this even though I don't have an API yet? I only have local data which are directly defined by me.
Currently, I am confused on where to begin using an onClick event, props, hooks for this one. Thus it would be great to get some helpful guides and tips for this functionality that I am working on.
It would indeed help me a lot as I am exploring React at the moment, thank you very much!!!