How to Create a Product Search and Filter Component in React with Tailwind CSS
- Date

-
admin
๐ How to Create a Product Search and Filter Component in React with Tailwind CSS
If you're building an e-commerce app or product listing page, having a search and filter feature is essential. In this tutorial, weโll build a clean and fully-functional Product Search Filter component in React, styled using Tailwind CSS.
๐ Features Covered
- ๐ Search by product name or description
- ๐ต Filter by price range
- ๐งฉ Filter by category
- ๐ Sort by name or price
- โก Reset filters with a single click
๐ ๏ธ Technologies Used
- React (
useState
Hook) - Tailwind CSS
๐ฆ Complete Code
Here is the complete ProductSearchFilter
component:
// ProductSearchFilter.jsx
import { useState } from 'react';
const ProductSearchFilter = ({ products }) => {
const [searchTerm, setSearchTerm] = useState('');
const [priceRange, setPriceRange] = useState([0, 1000]);
const [selectedCategory, setSelectedCategory] = useState('all');
const [sortOption, setSortOption] = useState('default');
const categories = ['all', ...new Set(products.map(product => product.category))];
const filteredProducts = products
.filter(product => {
const matchesSearch = product.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
product.description.toLowerCase().includes(searchTerm.toLowerCase());
const matchesPrice = product.price >= priceRange[0] && product.price <= priceRange[1];
const matchesCategory = selectedCategory === 'all' || product.category === selectedCategory;
return matchesSearch && matchesPrice && matchesCategory;
})
.sort((a, b) => {
if (sortOption === 'price-low') return a.price - b.price;
if (sortOption === 'price-high') return b.price - a.price;
if (sortOption === 'name-asc') return a.name.localeCompare(b.name);
if (sortOption === 'name-desc') return b.name.localeCompare(a.name);
return 0;
});
return (
<div className="container mx-auto px-4 py-8">
{/* UI elements go here */}
</div>
);
};
export default ProductSearchFilter;
๐ง How It Works
1. State Management with useState
We use Reactโs useState
to manage filters:
searchTerm
: keyword inputpriceRange
: array for min & max valuesselectedCategory
: current selected categorysortOption
: current selected sort option
2. Filtering Logic
Products are filtered by:
- Text matching in
name
ordescription
- Price within range
- Category match
3. Sorting Logic
Products can be sorted by:
- Price (low โ high, high โ low)
- Name (A โ Z, Z โ A)
4. Reset Filters
If no products match, a โReset Filtersโ button resets all inputs to default values.
๐ผ๏ธ Sample Props Format
Pass the products
prop as an array of objects like this:
const products = [
{
id: 1,
name: 'Smartphone',
description: 'Latest 5G smartphone',
price: 499,
category: 'electronics',
image: 'https://example.com/image.jpg'
},
// more products...
];
๐ฏ Final Thoughts
This ProductSearchFilter
component is powerful, clean, and perfect for any e-commerce frontend. You can further enhance it by:
- Adding a rating filter
- Integrating with API
- Making it mobile-friendly with better UX
If you liked this tutorial, donโt forget to share, comment, and subscribe for more awesome React content!