WDF Unit 3 Programms
WDF Unit 3 Programms
Gadgets.jsx
import React from 'react';
const Gadgets = ({ products }) => {
return (
<div>
<h1>Gadgets List</h1>
<ol>
{products.map((product, index) => (
<li
key={index}
style={{
border: product.price > 50000 ? '2px solid black' : 'none',
padding: '10px',
marginBottom: '10px',
}}
>
<h2>{product.name}</h2>
<p>{product.description}</p>
<p><strong>Price: </strong>${product.price}</p>
</li>
))}
</ol>
</div>
);
};
export default Gadgets;
App.js
const products = [
{
name: 'Smartphone',
description: 'Latest model with advanced features.',
price: 45000,
},
{
name: 'Laptop',
description: 'High-performance laptop for gaming and work.',
price: 60000,
},
{
name: 'Smartwatch',
description: 'Tracks your health and syncs with your phone.',
price: 20000,
},
];
function App() {
return (
<div>
<Gadgets products={products} />
</div>
);
}
2. How can you use state and props in a weather application to pass a location to a
component, update the location via a form input, and pass the updated location to
the child component?
return (
<div>
<h1>Weather Application</h1>
{/* Step 4: Pass the location to the child component via props */}
<WeatherDisplay location={location} />
</div>
);
};
return (
<div>
<h2>Weather in {location}</h2>
{weatherData ? (
<div>
<p>Temperature: {weatherData.current.temp_c}°C</p>
<p>Condition: {weatherData.current.condition.text}</p>
</div>
):(
<p>Loading weather data...</p>
)}
</div>
);
};
3. An e-commerce website built using React, where we can filter clothes products using
their customer preferred sizes as M, L or XL etc. We have a button called “Add to cart”
below each product shown on the web page, once user selects any product, it will go to
cart. At the end it can be used to checkout.
App.js
import React, { useState } from 'react';
import ProductList from './components/ProductList';
import Cart from './components/Cart';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
function App() {
const [cart, setCart] = useState([]);
// Function to add items to the cart
const addToCart = (product) => {
setCart([...cart, product]);
};
return (
<Router>
<div>
<h1>Welcome to the E-commerce Site</h1>
<Switch>
<Route exact path="/">
<ProductList addToCart={addToCart} />
</Route>
<Route path="/cart">
<Cart cart={cart} />
</Route>
</Switch>
</div>
</Router>
);
}
export default App;
ProductList.jsx
import React, { useState } from 'react';
import Product from './Product';
const products = [
{ id: 1, name: 'T-shirt', size: 'M', price: 20 },
{ id: 2, name: 'Jeans', size: 'L', price: 40 },
{ id: 3, name: 'Jacket', size: 'XL', price: 60 },
{ id: 4, name: 'Shoes', size: 'M', price: 50 },
];
const ProductList = ({ addToCart }) => {
const [selectedSize, setSelectedSize] = useState('All');
const handleSizeFilter = (event) => {
setSelectedSize(event.target.value);
};
const filteredProducts = selectedSize === 'All' ? products : products.filter(product => product.size
=== selectedSize);
return (
<div>
<div>
<label htmlFor="size-filter">Filter by size: </label>
<select id="size-filter" onChange={handleSizeFilter}>
<option value="All">All</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
</div>
<div className="product-list">
{filteredProducts.map((product) => (
<Product key={product.id} product={product} addToCart={addToCart} />
))}
</div>
</div>
);
};
export default ProductList;
Product.jsx
import React from 'react';
const Product = ({ product, addToCart }) => {
return (
<div className="product-card">
<h3>{product.name}</h3>
<p>Size: {product.size}</p>
<p>Price: ${product.price}</p>
<button onClick={() => addToCart(product)}>Add to Cart</button>
</div>
);
};
export default Product;
Cart.jsx
import React from 'react';
const Cart = ({ cart }) => {
const total = cart.reduce((acc, product) => acc + product.price, 0);
return (
<div>
<h2>Your Cart</h2>
<ul>
{cart.length === 0 ? (
<p>Your cart is empty</p>
):(
cart.map((item, index) => (
<li key={index}>
{item.name} - ${item.price}
</li>
))
)}
</ul>
{cart.length > 0 && (
<>
<p>Total: ${total}</p>
<button onClick={() => alert('Proceeding to checkout!')}>Checkout</button>
</>
)}
</div>
);
};
export default Cart;
CSS
.product-list {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.product-card {
border: 1px solid #ccc;
padding: 20px;
width: 150px;
}
button {
padding: 10px;
margin-top: 10px;
cursor: pointer;
}
4. How can you use state and props in a travel application to pass a location to a
component, update the location via a form input, and pass the updated location to the
child component?
App.js (Parent Component)
import React, { useState } from 'react';
import LocationDisplay from './LocationDisplay';
const App = () => {
// Step 1: Manage location state in the parent component
const [location, setLocation] = useState('');
input {
padding: 8px;
font-size: 16px;
}
h2 {
color: blue;
}