DEV Community

Cover image for Search Component in React
Chetan Rohilla
Chetan Rohilla

Posted on • Edited on • Originally published at w3courses.org

10 3

Search Component in React

Search Bar is a text-box used to search data in file or database based on user’s input. In our web or mobile applications we can create search component in react in different ways. Here below we will create react search component in class component.

Create Search Bar in React

class SearchComponent extends React.Component {

   state = { searchString: '' }

   handleChange = (e) => {
     this.setState({ searchString:e.target.value });
   }

   render() {

     var searchItems = this.props.items,
         searchString = this.state.searchString.trim().toLowerCase();

     if (searchString.length > 0) {
       searchItems = searchItems.filter(function(i) {
         return i.name.toLowerCase().match( searchString );
       });
     }

     return (
       <div>
          <input type="text" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here..."/>
          <ul>
            {searchItems.map(function(i) {
                return <li>{i.name} <a href={i.url}>{i.url}</a></li>;
            }) } 
          </ul>
       </div>
     );
   }
}

export default SearchComponent;
Enter fullscreen mode Exit fullscreen mode

Now, we have an <SearchComponent /> which we can use in our class or functional component and items is an prop for this component. For example, we are using <SearchComponent /> in the component below.

import {SearchComponent} from './search-component';

class HomePage extends React.Component {

    render() {

    // Search Items this can be static or through API
    var searchItems = [
        { name: 'AngularJS', url: 'https://angularjs.org/'},
        { name: 'jQuery', url: 'https://jquery.com/'},
        { name: 'React', url: 'https://facebook.github.io/react/'},
        { name: 'Express', url: 'http://expressjs.com/'},
        { name: 'PHP', url: 'http://w3courses.org/'},
        { name: 'Laravel', url: 'http://w3courses.org/'}
    ];

     return (
       <div>
            // put input and display on page
            <SearchComponent items={searchItems} />
       </div>
     );
   }

}

export default HomePage;
Enter fullscreen mode Exit fullscreen mode

You can also apply some CSS to to beautify style or icons.


Please like share subscribe and give positive feedback to motivate me to write more for you.

For more tutorials please visit my website.

Thanks:)
Happy Coding:)

Sentry mobile image

App store rankings love fast apps - mobile vitals can help you get there

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read full post →

Top comments (1)

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

14, good job keep going!

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay