component: add burger ingredients by props in the dynamic way

This commit is contained in:
oscarzhou 2020-12-09 22:42:33 +13:00
parent 0d456766b0
commit 8eeb04f654
2 changed files with 20 additions and 4 deletions

View File

@ -3,12 +3,19 @@ import React from 'react'
import classes from './Burger.css'; import classes from './Burger.css';
import BurgerIngredient from './BurgerIngredient/BurgerIngredient'; import BurgerIngredient from './BurgerIngredient/BurgerIngredient';
const burger = () => { const burger = (props) => {
const transformedIngredients = Object.keys(props.ingredients)
.map(igKey => {
return [...Array(props.ingredients[igKey])].map( (_, i) => {
return <BurgerIngredient key={igKey + i} type={igKey} />;
})
});
return ( return (
<div className={classes.Burger}> <div className={classes.Burger}>
<BurgerIngredient type="bread-top" /> <BurgerIngredient type="bread-top" />
<BurgerIngredient type="meat" /> {transformedIngredients}
<BurgerIngredient type="cheese" />
<BurgerIngredient type="bread-bottom" /> <BurgerIngredient type="bread-bottom" />
</div> </div>
) )

View File

@ -5,10 +5,19 @@ import Burger from '../../components/Burger/Burger';
class BurgerBuilder extends Component { class BurgerBuilder extends Component {
state = {
ingredients: {
meat: 1,
cheese: 1,
salad: 2,
bacon: 2,
}
}
render() { render() {
return ( return (
<Aux> <Aux>
<Burger /> <Burger ingredients={this.state.ingredients} />
<div>Build Controls</div> <div>Build Controls</div>
</Aux> </Aux>
); );