rockwire
rockwire

Reputation: 95

How do I filter my searches?

So these are my movie cards.

These are the properties of each of the movie card

So this is the onKeyPress :

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

I need to pass that value into my function, this is what i have so far :

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

How would I pass the value of the input inside the function?

Upvotes: 0

Views: 93

Answers (1)

rowinbot
rowinbot

Reputation: 632

You should try this in your class:

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

And then call it like this.filterFavoriteMovies(evt.target.value)

Although I'll recommend you to handle it all in the function, it's not recommended to declare the function in your JSX (reason from the official docs)

It would look like this:

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

And in your JSX like: onKeyPress={this.filterFavoriteMovies}

Upvotes: 1

Related Questions