Search Filter with React Native on FlatList

前端 未结 10 1238
孤街浪徒
孤街浪徒 2020-12-28 08:33

I am trying to search through a flatlist based on a search bar text. The problem I am running into is that when the user mistypes...say they wanted to type \"burger\" but ty

10条回答
  •  梦谈多话
    2020-12-28 08:42

    My search method; from @metehan-senol

    search = (searchText) => {
     this.setState({searchText: searchText});
    
     let filteredData = this.state.data.filter(function (item) {
       return item.description.includes(searchText);
     });
    
     this.setState({filteredData: filteredData});
    };
    

    the search method of could be simplify and Eslint proof like so

    search = (searchText) => {
      const searched = searchText.toLowerCase();
      this.setState(prevState => ({
        searchText: searched,
        filteredData: prevState.data.filter(item =>
          item.description.toLowerCase().includes(searched)
        ),
      }));
    }; 
    

提交回复
热议问题