React Reusable Search Bar Component with Tailwind
Looking to add a search bar to your React app? In this guide you will find the code for clean and reusable search bar component using React and Tailwind CSS.
A search bar is a handy tool that lets users quickly find what they’re looking for by typing keywords. You’ll spot search bars everywhere on websites, apps, online stores, blogs, and dashboards. They’re super important because they save time, help users navigate large amounts of content, and improve the overall experience.
Important Features of the Search Bar Component
- Responsive Design – Works well on all screen sizes.
- Dynamic Input Handling – Updates search value as the user types.
- Search Icon for Clarity – Enhances user experience.
- Submit Button – Allows users to trigger a search easily.
- Tailwind CSS Styling – Clean and customizable design.
React Tailwind Search Bar UI
Below is a screenshot of the Tailwind ad banner UI to give you a clear idea of its design and layout:
React Search Bar Component Code
Copy the code below and add a polished search experience to your app!
API
Prop | Type | Required | Default | Description |
---|---|---|---|---|
onSubmit | (event: FormEvent<HTMLFormElement>) => void | Yes | undefined | Function to handle form submission. |
value | string | Yes | undefined | Current value of the search input. |
onChange | (event: ChangeEvent<HTMLInputElement>) => void | Yes | undefined | Function to handle input value changes. |
placeholder | string | No | "Search..." | Placeholder text for the search input. |
How to use in App.tsx
1import { useState, FormEvent, ChangeEvent } from 'react' 2import SearchBar from './components/SearchBar' 3 4const App = () => { 5 const [searchTerm, setSearchTerm] = useState('') 6 7 const handleSearchChange = (event: ChangeEvent<HTMLInputElement>) => { 8 setSearchTerm(event.target.value) 9 } 10 11 const handleSearchSubmit = (event: FormEvent<HTMLFormElement>) => { 12 event.preventDefault() 13 console.log('Searching for:', searchTerm) 14 // Perform search logic here 15 } 16 17 return ( 18 <div className="flex min-h-screen items-center justify-center bg-gray-100 p-4"> 19 <SearchBar 20 onSubmit={handleSearchSubmit} 21 value={searchTerm} 22 onChange={handleSearchChange} 23 placeholder="Search for something..." 24 /> 25 </div> 26 ) 27} 28 29export default App
Do you have a component request? Feel free to contact me on LinkedIn.