React Product Card Component using Props and Styling

๐Ÿ“Œ INTRODUCTION

In modern web applications, displaying products in a card format is very common (e-commerce, dashboards, portfolios).

In this program, we will:

  • Create a reusable Product Card Component
  • Use props to pass product data
  • Apply styling for better UI

๐Ÿ‘‰ This is a real-world React component and very useful for projects.


๐Ÿ“˜ PROBLEM STATEMENT

๐Ÿ‘‰ Develop a React component to display a product card, accepting props like productName, price, and imageUrl.


๐Ÿ’ก CONCEPTS USED

  • React Props
  • Functional Components
  • Inline Styling / CSS
  • Component Reusability

๐Ÿง  LOGIC EXPLANATION

  1. Create a ProductCard component
  2. Pass product data via props
  3. Display image, name, and price
  4. Apply styling for card layout

๐Ÿ’ป PROGRAM CODE


Step 1: Create ProductCard Component

function ProductCard({ productName, price, imageUrl }) {
return (
<div style={styles.card}>
<img src={imageUrl} alt={productName} style={styles.image} />
<h2>{productName}</h2>
<p>Price: โ‚น{price}</p>
<button style={styles.button}>Buy Now</button>
</div>
);
}

const styles = {
card: {
border: "1px solid #ddd",
borderRadius: "10px",
padding: "15px",
width: "200px",
textAlign: "center",
boxShadow: "0 4px 8px rgba(0,0,0,0.1)",
margin: "10px"
},
image: {
width: "100%",
height: "150px",
objectFit: "cover"
},
button: {
backgroundColor: "#007bff",
color: "white",
border: "none",
padding: "8px 12px",
borderRadius: "5px",
cursor: "pointer"
}
};

export default ProductCard;

Step 2: Use in App.js

import ProductCard from "./ProductCard";

function App() {
return (
<div style={{ display: "flex" }}>
<ProductCard
productName="Smartphone"
price="15000"
imageUrl="https://via.placeholder.com/150"
/>

<ProductCard
productName="Headphones"
price="2000"
imageUrl="https://via.placeholder.com/150"
/>
</div>
);
}

export default App;

๐Ÿ–ฅ๏ธ OUTPUT

๐Ÿ‘‰ You will see:

[Product Card 1]   [Product Card 2]

Smartphone Headphones
โ‚น15000 โ‚น2000
[Buy Now] [Buy Now]

๐Ÿ” STEP-BY-STEP EXPLANATION

  • Props (productName, price, imageUrl) โ†’ pass product data
  • <img /> โ†’ displays product image
  • Inline styles โ†’ design card layout
  • Component reused multiple times

โšก ADVANCED VERSION (USING CSS FILE)

styles.css

.card {
border: 1px solid #ddd;
border-radius: 10px;
padding: 15px;
width: 200px;
text-align: center;
}

๐ŸŽฏ WHY THIS COMPONENT IS IMPORTANT

  • Used in e-commerce websites
  • Demonstrates props + UI design
  • Reusable and scalable
  • Helps build portfolio projects

๐ŸŒŸ IMPROVEMENTS YOU CAN ADD

  • Add rating โญ
  • Add discount %
  • Add “Add to Cart” functionality
  • Fetch products from API

๐Ÿ“š REAL-WORLD USE CASE

  • Amazon / Flipkart product cards
  • Dashboard widgets
  • Portfolio projects
  • Shopping apps

โ“ VIVA QUESTIONS (IMPORTANT)

Q1. What are props in React?

๐Ÿ‘‰ Props are used to pass data to components.


Q2. Why use reusable components?

๐Ÿ‘‰ To avoid code duplication and improve scalability.


Q3. What is inline styling in React?

๐Ÿ‘‰ Styling using JavaScript objects.


Q4. Can we style using CSS files?

๐Ÿ‘‰ Yes, both inline and external CSS can be used.


Q5. What is component reusability?

๐Ÿ‘‰ Using the same component multiple times with different data.


๐Ÿ”— RELATED POSTS

๐Ÿ‘‰ MCA 168 Full Stack Development Lab Programs List
๐Ÿ‘‰ React Props โ€“ Program 10
๐Ÿ‘‰ React Counter App โ€“ Program 6
๐Ÿ‘‰ React List Rendering โ€“ Program 7


๐Ÿ“Œ CONCLUSION

The Product Card component is a foundational building block for real-world React applications.

๐Ÿ‘‰ Master this to start building full-scale projects like e-commerce apps.


Further Reading

JUnit Tutorial

How to Master Full Stack Development?

Spring Framework Practice Problems and Their Solutions

30 MCQs on JUnit

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

Java Practice Exercise

programmingempire

Princites

Leave a Reply

Your email address will not be published. Required fields are marked *