I am trying to filter an array of coffee based on its origin via a button click in React. Right now, whenever a button is clicked the array goes to 0 instead of the desired result of however many items contain the origin clicked. Thanks for your help. Feel free to critique my code as well - I am just beginning!
class Coffee extends React.Component {
constructor() {
super();
this.state = { coffees: [
{
name: 'Banko',
origin: 'Ethiopia',
price: 16
},
{
name: 'Pueblo',
origin: 'Costa Rica',
price: 14
},
{
name: 'Don Pablo',
origin: 'Columbia',
price: 13
},
{
name: 'Hava Lama',
origin: 'Columbia',
price: 15
},
{
name: 'Guillermo Salva',
origin: 'Ethiopia',
price: 19
},
{
name: 'Salla Rosa',
origin: 'Kenya',
price: 20
},
{
name: 'Santor Gustavio',
origin: 'Panama',
price: 25
}
]
}
}
handleClick = event => {
const byOrigin = event.target.value
const filterCoffee = this.state.coffees.filter(coffee => coffee[origin] === [byOrigin])
this.setState({coffees: filterCoffee})
}
render() {
const renderAll = this.state.coffees.map(coffee => <li key={Date.now()}>{coffee.name}</li>)
return (
<div>
<button value='all' onClick={this.handleClick}>All</button>
<button value='Ethiopia' onClick={this.handleClick}>Ethiopia</button>
<button value='Costa Rica' onClick={this.handleClick}>Costa Rica</button>
<button value='Kenya' onClick={this.handleClick}>Kenya</button>
<button value='Columbia' onClick={this.handleClick}>Columbia</button>
<button value='Panama' onClick={this.handleClick}>Panama</button>
<p>Coffee: {renderAll}</p>
<p>{this.state.coffees.length}</p>
</div>
)
}
}
ReactDOM.render(<Coffee />, document.getElementById('root'))
coffee[origin] === [byOrigin].this.state.coffees.filter(coffee => coffee[origin] === byOrigin).filter(coffee => coffee.origin === byOrigin)