Skip to content

Instantly share code, notes, and snippets.

@mohandere
Last active May 12, 2019 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohandere/24ad3ad78cfa5d5b77b0ef37a1b47e3b to your computer and use it in GitHub Desktop.
Save mohandere/24ad3ad78cfa5d5b77b0ef37a1b47e3b to your computer and use it in GitHub Desktop.
Higher-Order Component
import products from './products.json'
class ProductsListWithSearch extends React.PureComponent {
state = {
searchTerm: ''
}
handleSearch = event => {
this.setState({ searchTerm: event.target.value })
}
render() {
const { searchTerm } = this.state
let filteredProducts = filterProducts(searchTerm);
return (
<>
<input onChange={this.handleSearch} value={this.state.searchTerm} type="text" placeholder="Search" />
<ProductsList products={filteredProducts} />
</>
)
}
const filterProducts = (searchTerm) => {
searchTerm = searchTerm.toUpperCase()
return products.filter(product => {
let str = `${product.title} ${product.style} ${product.sku}`.toUpperCase();
return str.indexOf(searchTerm) >= 0;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment