React E-commerce website
React E-commerce website
Features:
View Product Details: Users can click on a product to see more details.
sh
குறியீட்டை நகலெடுக்கவும்
cd ecommerce-website
npm start
Code:
1. App.js
javascript
குறியீட்டை நகலெடுக்கவும்
function App() {
{ id: 1, name: 'Product 1', price: 100, description: 'Description of Product 1'
},
{ id: 2, name: 'Product 2', price: 200, description: 'Description of Product 2'
},
]);
setCart([...cart, product]);
};
};
return (
<div className="App">
<h1>E-commerce Website</h1>
{isCheckout ? (
) : selectedProduct ? (
):(
<ProductList products={products}
onProductSelect={setSelectedProduct} />
)}
</div>
);
2. ProductList.js
javascript
குறியீட்டை நகலெடுக்கவும்
return (
<div>
{products.map(product => (
))}
</div>
);
3. ProductItem.js
javascript
குறியீட்டை நகலெடுக்கவும்
return (
<h3>{product.name}</h3>
<p>${product.price}</p>
</div>
);
4. ProductDetail.js
javascript
குறியீட்டை நகலெடுக்கவும்
return (
<div>
<button onClick={onBack}>Back</button>
<h2>{product.name}</h2>
<p>{product.description}</p>
<p>${product.price}</p>
</div>
);
5. Cart.js
javascript
குறியீட்டை நகலெடுக்கவும்
return (
<div>
<h2>Shopping Cart</h2>
{cart.length === 0 ? (
cart.map(product => (
<div key={product.id}>
<h3>{product.name}</h3>
<p>${product.price}</p>
</div>
))
)}
</div>
);
6. Checkout.js
javascript
குறியீட்டை நகலெடுக்கவும்
return (
<div>
<h2>Checkout</h2>
{cart.map(product => (
<div key={product.id}>
<h3>{product.name}</h3>
<p>${product.price}</p>
</div>
))}
<h3>Total: ${total}</h3>
<button>Proceed to Payment</button>
</div>
);
Explanation:
State Management:
isCheckout: A boolean to toggle between the checkout view and other views.
Components:
App: Manages the state and renders the appropriate components based on
the current state.
Cart: Displays the products in the shopping cart and handles removal and
checkout.
Checkout: Displays the checkout details and the total price of items in the
cart.
Props:
Event Handling:
Adding a product to the cart calls the onAddToCart function, which updates
the cart state in App.
This project covers various React.js concepts and provides a solid foundation
for implementing more advanced features like user authentication, dynamic
product fetching, and payment integration.