Search code examples
react-nativesearchbarreact-native-flatlist

Implementing a Search bar using FlatList in React Native


I'm developing a React Native application which consists of FlatList. I referred this article, https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6 to use FlatList component to my application.

I would like to implement search bar to search the contents of that list (using the titles of each item) & automatically render the content according to the search text. How can I do this without using any libraries?

Thanks for your help!


Solution

  • Check below example which i created using flatlist and TextInput. Items are displayed in the form of a dropdown list when you search items. i think this will help you.

    import React, { Component } from 'react';
    import { View, Text, FlatList, TextInput, ListItem } from 'react-native';
    
    class SearchFunction extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          data: [],
          value: '',
        };
    
        this.arrayNew = [
          { name: 'Robert' },
          { name: 'Bryan' },
          { name: 'Vicente' },
          { name: 'Tristan' },
          { name: 'Marie' },
          { name: 'Onni' },
          { name: 'sophie' },
          { name: 'Brad' },
          { name: 'Samual' },
          { name: 'Omur' },
          { name: 'Ower' },
          { name: 'Awery' },
          { name: 'Ann' },
          { name: 'Jhone' },
          { name: 'z' },
          { name: 'bb' },
          { name: 'cc' },
          { name: 'd' },
          { name: 'e' },
          { name: 'f' },
          { name: 'g' },
          { name: 'h' },
          { name: 'i' },
          { name: 'j' },
          { name: 'k' },
          { name: 'React' },
          { name: 'React native' },
          { name: 'react navigations' },
        ];
      }
    
      renderSeparator = () => {
        return (
          <View
            style={{
              height: 1,
              width: '100%',
              backgroundColor: '#CED0CE',
            }}
          />
        );
      };
    
      searchItems = text => {
        let newData = this.arrayNew.filter(item => {
          const itemData = `${item.name.toUpperCase()}`;
          const textData = text.toUpperCase();
        if(text.length >0 ){
          return itemData.indexOf(textData) > -1;
        }
        });
        this.setState({
          data: newData,
          value: text,
        });
      };
    
      renderHeader = () => {
        return (
          <TextInput
            style={{ height: 60, borderColor: '#000', borderWidth: 1 }}
            placeholder="   Type Name..."
            onChangeText={text => this.searchItems(text)}
            value={this.state.value}
          />
        );
      };
    
      render() {
        return (
          <View
            style={{
              flex: 1,
              padding: 25,
              width: '98%',
              alignSelf: 'center',
              justifyContent: 'center',
            }}>
            <FlatList
              data={this.state.data}
              renderItem={({ item }) => (
                <Text style={{ padding: 10 }}>{item.name} </Text>
              )}
              keyExtractor={item => item.name}
              ItemSeparatorComponent={this.renderSeparator}
              ListHeaderComponent={this.renderHeader}
            />
          </View>
        );
      }
    }
    
    export default SearchFunction;