I am making a food app that when the button on each component is it open on another page and the URL also changes .so I am using react routers but when I click the button it does not open that component
This is App.js
import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import About from "./About"
import Contact from "./Contact"
import Home from "./Home"
import Menu from "./Menu"
function App() {
return (
<div>
<div>
<div className="navbar">
<h3>Food Festive</h3>
<div>
<ul>
<li>
<Link className="chg" to="/home">Home</Link>
</li>
<li>
<Link className="chg" to="/about">About</Link>
</li>
<li>
<Link className="chg" to="/contact">Contact</Link>
</li>
<li>
<Link className="chg" to="/menu">Menu</Link>
</li>
</ul>
</div>
</div>
<Switch>
<Router path="/home">
<Home />
</Router>
<Router path="/about">
<About />
</Router>
<Router path="/contact">
<Contact />
</Router>
<Router path="/menu">
<Menu />
</Router>
</Switch>
</div>
</div>
);
}
export default App;
the CardItems.js here I want that when I click Button it opens ButtonClick.js separately. but when I click it it displays after the button like this:
import React from "react";
import { Card, Button, CardDeck } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import ButtonClick from "./ButtonClick"
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
function CardItems(props) {
function name() {
console.log(`${props.item.name}`)
}
return (
<div className="wrap">
<Card>
<Card.Img variant="top" src={require(`${props.item.img}`)} />
<Card.Body>
<h5>{props.item.name}</h5>
<div>
<h6>Price : ${props.item.price} </h6>
<h6>Orders till now : {props.item.Orders} </h6>
<h6>Home Delivery Time : {props.item.Delivery} </h6>
<h6>Quantity : {props.item.Quantity}</h6>
</div>
<Link to="/newpage">
<Button variant="info" onClick={name}>
Details
</Button>
</Link>
<Switch>
<Router path="/newpage">
<ButtonClick />
</Router>
</Switch>
</Card.Body>
</Card >
</div>
);
}
export default CardItems;
CardItems array
const CardItemsArray = [
{
id: 1,
img: "./images/Food/cupcakes.jpg",
name: "Cupcakes",
price: 20,
Orders: 3025,
Delivery: "45mins",
Quantity: "6 Cup-cakes",
},
{
id: 2,
img: "./images/Food/fried-chicken.jpg",
name: "Fried Chicken",
price: 30,
Orders: 3001,
Delivery: "45mins",
Quantity: "3 Pieces",
},
{
id: 3,
img: "./images/Food/pizza23.jpg",
name: "Pizza",
price: 50,
Orders: 5029,
Delivery: "45mins",
Quantity: "1 Pizza",
},
{
id: 4,
img: "./images/Food/instant-nood.jpg",
name: "Instant Noodles",
price: 10,
Orders: 1042,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 5,
img: "./images/Food/hamburger.jpg",
name: "Hamburger",
price: 40,
Orders: 2134,
Delivery: "45mins",
Quantity: "1 Burgur",
},
{
id: 6,
img: "./images/Food/sushi.jpg",
name: "Shushi",
price: 100,
Orders: 1872,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 7,
img: "./images/Food/ice.jpg",
name: "Ice-Cream",
price: 10,
Orders: 1272,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 8,
img: "./images/Food/chicken-stack.jpg",
name: "Beef Steack",
price: 70,
Orders: 2381,
Delivery: "45mins",
Quantity: "1 Piece",
},
{
id: 9,
img: "./images/Food/fries.jpg",
name: "Fries",
price: 15,
Orders: 4231,
Delivery: "45mins",
Quantity: "1 Pack",
},
{
id: 10,
img: "./images/Food/ice-cups.jpg",
name: "Ice-Cream-Cups",
price: 20,
Orders: 2836,
Delivery: "45mins",
Quantity: "2 Cups",
},
{
id: 11,
img: "./images/Food/strawb-smo.jpg",
name: "Strawberry Smoothie",
price: 30,
Orders: 2981,
Delivery: "45mins",
Quantity: "1 Glass",
},
{
id: 12,
img: "./images/Food/salad2.jpg",
name: "Salad",
price: 20,
Orders: 2311,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 13,
img: "./images/Food/orange juice.jpg",
name: "Orange Juice",
price: 15,
Orders: 1637,
Delivery: "45mins",
Quantity: "1 Glass",
},
{
id: 14,
img: "./images/Food/fruits-beauti.jpg",
name: "Fruit Salad",
price: 30,
Orders: 1091,
Delivery: "45mins",
Quantity: "1 Plate",
},
{
id: 15,
img: "./images/Food/tea1.jpg",
name: "Ice Tea",
price: 20,
Orders: 2348,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 16,
img: "./images/Food/tea2.jpg",
name: "Mint Tea",
price: 20,
Orders: 2536,
Delivery: "45mins",
Quantity: "1 Cup",
},
{
id: 17,
img: "./images/Food/bread.jpg",
name: "Bread Cuisine",
price: 40,
Orders: 975,
Delivery: "45 mins",
Quantity: "1 Plate",
},
{
id: 18,
img: "./images/Food/lime-juice.jpg",
name: "Lime Juice",
price: 20,
Orders: 6437,
Delivery: "45 mins",
Quantity: "1 Glass",
},
{
id: 19,
img: "./images/Food/pizza2.jpg",
name: "Fagita Pizza",
price: 100,
Orders: 1862,
Delivery: "45 mins",
Quantity: "1 Pizza",
},
{
id: 20,
img: "./images/Food/cake2.jpg",
name: "Cake",
price: 150,
Orders: 2154,
Delivery: "45 mins",
Quantity: "1 Cake",
},
];
export default CardItemsArray;
menu.js
import React from "react";
import CardItems from "./CardItems.js";
import CardItemsArray from "./CardItemsArray";
function Menu() {
const items = CardItemsArray.map((item) => (
<CardItems key={item.id} item={item} />
));
return (
<div>
<div>{items}</div>
</div>
);
}
export default Menu;
ButtonClick.js
import React from "react";
function ButtonClick(props) {
return (
<div>
<h1>Button is Clicked</h1>
</div>
);
}
export default ButtonClick;