component: provide the hint when there is no ingredients by merging multiple arrays into one

This commit is contained in:
oscarzhou 2020-12-10 19:08:43 +13:00
parent 8eeb04f654
commit b4fbdade6d
2 changed files with 12 additions and 6 deletions

View File

@ -5,13 +5,19 @@ import BurgerIngredient from './BurgerIngredient/BurgerIngredient';
const burger = (props) => {
const transformedIngredients = Object.keys(props.ingredients)
let transformedIngredients = Object.keys(props.ingredients)
.map(igKey => {
return [...Array(props.ingredients[igKey])].map( (_, i) => {
return <BurgerIngredient key={igKey + i} type={igKey} />;
})
});
})
.reduce((arr, el) => { // merge multiple arrays into one
return arr.concat(el);
}, []);
if (transformedIngredients.length === 0){
transformedIngredients = <p>Please start adding ingredients</p>;
}
return (
<div className={classes.Burger}>
<BurgerIngredient type="bread-top" />

View File

@ -7,10 +7,10 @@ class BurgerBuilder extends Component {
state = {
ingredients: {
meat: 1,
cheese: 1,
salad: 2,
bacon: 2,
meat: 0,
cheese: 0,
salad: 0,
bacon: 0,
}
}