How to create Shopping Cart Button in ReactJS?
Last Updated :
24 Jul, 2024
A shopping cart button is one of the most used component in e-commerce websites or applications which allows users to add items to their cart . In this tutorial, we will learn how to create a shopping cart button in ReactJS using Material-UI.
Prerequisites
Steps to create React Application And Installing Required Modules:
Step 1: Create a React application using the following command.
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command.
cd foldername
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command.
npm install @material-ui/core
npm install @material-ui/icons
Project Structure:
The updated dependencies in package.json file will look like:
"dependencies": {
"@material-ui/core": "^4.12.4",
"@material-ui/icons": "^4.11.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
Example: Now write down the following code in the in respective file.
CSS
/* ShoppingCart.css */
.shopping-cart {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
.cart-items {
width: 100%;
max-width: 600px;
margin-bottom: 20px;
}
.cart-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
margin-bottom: 10px;
}
.item-controls {
display: flex;
align-items: center;
}
.item-controls button {
background-color: #007bff;
color: white;
border: none;
padding: 5px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 0 5px;
transition: background-color 0.3s;
}
.item-controls button:hover {
background-color: #0056b3;
}
.item-price {
font-size: 16px;
font-weight: bold;
}
.cart-total {
font-size: 20px;
font-weight: bold;
margin-bottom: 20px;
}
.item-list {
display: flex;
flex-direction: column;
align-items: center;
}
.item-list button {
background-color: #007bff;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 5px;
transition: background-color 0.3s;
}
.item-list button:hover {
background-color: #0056b3;
}
JavaScript
// App.js
import React from 'react';
import ShoppingCart from './ShoppingCart';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Shopping Cart Example</h1>
<ShoppingCart />
</header>
</div>
);
}
export default App;
JavaScript
// ShoppingCart.js
import React, { useState } from 'react';
import './ShoppingCart.css';
const ShoppingCart = () => {
const [cart, setCart] = useState([]);
const [total, setTotal] = useState(0);
const items = [
{ id: 1, name: 'Item 1', price: 10 }
];
const addItem = (item) => {
const existingItem = cart.find((cartItem) => cartItem.id === item.id);
if (existingItem) {
setCart(
cart.map((cartItem) =>
cartItem.id === item.id
? { ...cartItem, quantity: cartItem.quantity + 1 }
: cartItem
)
);
} else {
setCart([...cart, { ...item, quantity: 1 }]);
}
setTotal(total + item.price);
};
const removeItem = (item) => {
const existingItem = cart.find((cartItem) => cartItem.id === item.id);
if (existingItem.quantity === 1) {
setCart(cart.filter((cartItem) => cartItem.id !== item.id));
} else {
setCart(
cart.map((cartItem) =>
cartItem.id === item.id
? { ...cartItem, quantity: cartItem.quantity - 1 }
: cartItem
)
);
}
setTotal(total - item.price);
};
return (
<div className="shopping-cart">
<h2>Shopping Cart</h2>
{cart.length === 0 ? (
<p>Your cart is empty</p>
) : (
<div className="cart-items">
{cart.map((item) => (
<div key={item.id} className="cart-item">
<span>{item.name}</span>
<div className="item-controls">
<button onClick={() => removeItem(item)}>-</button>
<span>{item.quantity}</span>
<button onClick={() => addItem(item)}>+</button>
</div>
<span className="item-price">${item.price * item.quantity}</span>
</div>
))}
</div>
)}
<div className="cart-total">
<span>Total: ${total}</span>
</div>
<div className="item-list">
<h3>Items for Sale</h3>
{items.map((item) => (
<button key={item.id} onClick={() => addItem(item)}>
Add {item.name} - ${item.price}
</button>
))}
</div>
</div>
);
};
export default ShoppingCart;
Step to Run Application:
Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Similar Reads
How to create Like Button Using React JS ? This is a social media world and the like button is one of the main components of it. From social media accounts to shopping platforms like buttons are everywhere. In this tutorial, we'll explore how to create a dynamic button in a React application. The button will change its appearance based on di
4 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to Create Button in React-Native App ? React Native provides pre-defined components like button and TouchableOpacity to create buttons in a react native app. In this article, we will see how to create buttons in react-native, their syntax, and different types of buttons available in react-native.Table of ContentApproachButton in React Na
4 min read
How To Create a Website in ReactJS? ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS.PrerequisitesNPM & Node.jsR
5 min read
How to create Dialog Box in ReactJS? In modern web development, creating interactive and user-friendly interfaces is essential. One common element found in many applications is a dialog box. A dialog box is a component that appears on top of the main content to display information, receive user input, or prompt for confirmation. In thi
2 min read
How to create Dialog Box in ReactJS? Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks. Material UI for React has this component available for us and it is very easy to integrate. We can create the dialog box in ReactJS using the following approach:Creating React Applic
2 min read