我有一个电子商务应用程序与React.js,现在试图使排序的产品名称和价格(最低到最高和最高到最低)。我的产品是对象的关键字包括“名称”和“价格”。
我的代码如下:
App.js:
const [products, setProducts] = React.useState([
{ key: 1, name: "Airpods Pro", price: 79.99, image: "img/products/1.png" },
{ key: 2, name: "Hama Fun", price: 9.99, image: "img/products/2.jpg" },
{
key: 3,
name: "Panasonic RP-HS46E",
price: 19.99,
image: "img/products/3.jpg",
},
]);
return (
<div className="App">
<Routes>
<Route
path="/"
element={
<Home
products={products}
addToCart={addToCart}
addToFavorites={addToFavorites}
/>
}
/>
<Route path="/deals" element={<Deals deals={deals} />} />
<Route path="/new" element={<New />} />
<Route path="/account" element={<Account favorites={favorites} />} />
</Routes>
</div>
);
}
Home.jsx:
const Home = ({ products, addToCart, addToFavorites, }) => {
return (
<>
<TopBar />
<Header />
<div className="wrapper">
<Hero />
<Products products={products} addToCart={addToCart} addToFavorites={addToFavorites} />
</div>
</>
);
};
Products.js:
export const Products = ({ products, addToCart, addToFavorites }) => {
return (
<div className="products">
<div className="top-block">
<h1>Headphones For You!</h1>
<select>
<option value="Sort by" disabled>
Sort by
</option>
<option value="Name">Name</option>
<option value="Price: Lowest to highest">
Price: Lowest to highest
</option>
<option value="Price: Highest to lowest">
Price: Highest to lowest
</option>
</select>
</div>
<div className="products-card">
{products.map((item) => (
<Card
key={item.key}
cardKey={item.key}
name={item.name}
price={item.price}
image={item.image}
addToCart={(product) => addToCart(product)}
addToFavorites={(product) => addToFavorites(product)}
/>
))}
</div>
</div>
);
};
我需要将排序函数连接到选项。
先谢谢你的帮助。
1条答案
按热度按时间bjg7j2ky1#