Search Filter with React Native on FlatList

前端 未结 10 1234
孤街浪徒
孤街浪徒 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 09:01

    Here is my solution:

    You need to have a backup of your data

    this.state = {
        data: [],
        backup: []
    }
    

    on search method

    search = txt => {
        let text = txt.toLowerCase()
        let tracks = this.state.backup
        let filterTracks = tracks.filter(item => {
        if(item.name.toLowerCase().match(text)) {
          return item
        }
      })
      this.setState({ data: filterTracks })
    }
    

    Explanation: when calling setState on your data it will changed to current state and cannot be changed again.

    So backup data will handle to filter your data.

提交回复
热议问题