Skip to content

Instantly share code, notes, and snippets.

@franciscoandres
Last active February 24, 2019 18:35
Show Gist options
  • Save franciscoandres/ed40628621e708ea88c4cc3fec297c28 to your computer and use it in GitHub Desktop.
Save franciscoandres/ed40628621e708ea88c4cc3fec297c28 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
const foods = [
{
name: 'Pabellon',
type: 'lunch'
},
{
name: 'Empadanda',
type: 'breakfast'
},
{
name: 'Arepa',
type: 'breakfast'
}
];
function App () {
const [myFoods, setFoods] = useState(foods);
const filterByType = (type) => setFoods(() => foods.slice().filter(food => food.type === type || type === 'all'));
return (
<div>
<div>
<button onClick={() => filterByType('all')}>all</button>
<button onClick={() => filterByType('breakfast')}>breakfast</button>
<button onClick={() => filterByType('lunch')}>lunch</button>
</div>
<div>
{myFoods ? myFoods.map((food, index) =>
<p key={index}>{food.name}</p>
) : (
<p>what.the.hell</p>
)}
</div>
</div>
)
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment