如何过滤搜索?

时间:2018-06-30 23:45:47

标签: javascript reactjs

So these are my movie cards.

These are the properties of each of the movie card

这就是onKeyPress:

onKeyPress={ onFavoritesSearch(event.target.value)}  

我需要将该值传递到我的函数中,这就是我到目前为止的内容:

    onFavoritesSearch = (event) => {
    if (event.key === "Enter") {
        this.state.sortedFavorites.filter(movie => movie.title.indexOf(event.target.value) === 0)
      }
} 

如何在函数内部传递输入值?

1 个答案:

答案 0 :(得分:1)

您应该在课堂上尝试以下方法:

filterFavoriteMovies = (search) => {
  this.state.favorites.filter(movie => movie.title.indexOf(search) === 0)
}

然后将其命名为this.filterFavoriteMovies(evt.target.value)

尽管我建议您在函数中全部处理,但不建议在JSX(reason from the official docs)中声明函数

它看起来像这样:

filterFavoriteMovies = (event) => {
  if (event.key === "Enter") {
    this.state.favorites.filter(movie => movie.title.indexOf(event.target.value) === 0)
  }
}

在您的JSX中,例如:onKeyPress={this.filterFavoriteMovies}