Search code examples
react-nativesearchexpolodash

Searching in Expo's FlatList


I used this tutorial but it didn't work (if you are interested, check out my last post). Any suggestions to make a working search for a flatlist? I have a list of 100 things and just by inserting the name in a search bar, the flatlist should update with the results.


Solution

  • Try using react-native-searchable-flatlist

    import React, { Component } from 'react';
    import { View, Text, FlatList, ActivityIndicator } from 'react-native';
    import { ListItem, SearchBar } from 'react-native-elements';
    
    class FlatListDemo extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          loading: false,
          data: [],
          error: null,
        };
    
        this.arrayholder = [];
      }
    
      componentDidMount() {
        this.makeRemoteRequest();
      }
    
      makeRemoteRequest = () => {
        const url = `https://randomuser.me/api/?&results=20`;
        this.setState({ loading: true });
    
        fetch(url)
          .then(res => res.json())
          .then(res => {
            this.setState({
              data: res.results,
              error: res.error || null,
              loading: false,
            });
            this.arrayholder = res.results;
          })
          .catch(error => {
            this.setState({ error, loading: false });
          });
      };
    
      renderSeparator = () => {
        return (
          <View
            style={{
              height: 1,
              width: '86%',
              backgroundColor: '#CED0CE',
              marginLeft: '14%',
            }}
          />
        );
      };
    
      searchFilterFunction = text => {
        this.setState({
          value: text,
        });
    
        const newData = this.arrayholder.filter(item => {
          const itemData = `${item.name.title.toUpperCase()} ${item.name.first.toUpperCase()} ${item.name.last.toUpperCase()}`;
          const textData = text.toUpperCase();
    
          return itemData.indexOf(textData) > -1;
        });
        this.setState({
          data: newData,
        });
      };
    
      renderHeader = () => {
        return (
          <SearchBar
            placeholder="Type Here..."
            lightTheme
            round
            onChangeText={text => this.searchFilterFunction(text)}
            autoCorrect={false}
            value={this.state.value}
          />
        );
      };
    
      render() {
        if (this.state.loading) {
          return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
              <ActivityIndicator />
            </View>
          );
        }
        return (
          <View style={{ flex: 1 }}>
            <FlatList
              data={this.state.data}
              renderItem={({ item }) => (
                <ListItem
                  leftAvatar={{ source: { uri: item.picture.thumbnail } }}
                  title={`${item.name.first} ${item.name.last}`}
                  subtitle={item.email}
                />
              )}
              keyExtractor={item => item.email}
              ItemSeparatorComponent={this.renderSeparator}
              ListHeaderComponent={this.renderHeader}
            />
          </View>
        );
      }
    }
    
    export default FlatListDemo;