我有一个名为burger builder的反应项目 . 汉堡由4种成分沙拉,培根,奶酪和Meet组成,用户可以根据需要插入多种成分, What i want is whenever user add any ingredient more than 1 a prom should appear and ask at what position ingredients to be add ? For example if user add 2nd meet promot should ask where to the meet at top OR bottom OR middle?
我很困惑如何做到这一点,所以我想要一些帮助 .
import React, { Component } from "react";
import Aux from "../../hoc/Aux";
import Burger from "../../components/Burger/Burger";
import BuildControls from "../../components/Burger/BuildControls/BuildControls";
import Modal from "../../components/UI/Modal/Modal";
import OrderSummary from "../../components/Burger/OrderSummary/OrderSummary";
const INGREDIENT_PRICES = {
salad: 0.5,
cheese: 0.4,
meat: 1.3,
bacon: 0.7,
};
class BurgerBuilder extends Component {
state = {
ingredients: {
salad: 0,
bacon: 0,
cheese: 0,
meat: 0,
},
totalPrice: 4,
purchasable: false,
purchasing: false,
};
updatePurchaseState(ingredients) {
const sum = Object.keys(ingredients)
.map(igKey => {
return ingredients[igKey];
})
.reduce((sum, el) => {
return sum + el;
}, 0);
this.setState({ purchasable: sum > 0 });
}
addIngredientHandler = type => {
const oldCount = this.state.ingredients[type];
const updatedCount = oldCount + 1;
const updatedIngredients = {
...this.state.ingredients,
};
updatedIngredients[type] = updatedCount;
const priceAddition = INGREDIENT_PRICES[type];
const oldPrice = this.state.totalPrice;
const newPrice = oldPrice + priceAddition;
this.setState({ totalPrice: newPrice, ingredients: updatedIngredients });
this.updatePurchaseState(updatedIngredients);
};
removeIngredientHandler = type => {
const oldCount = this.state.ingredients[type];
if (oldCount <= 0) {
return;
}
const updatedCount = oldCount - 1;
const updatedIngredients = {
...this.state.ingredients,
};
updatedIngredients[type] = updatedCount;
const priceDeduction = INGREDIENT_PRICES[type];
const oldPrice = this.state.totalPrice;
const newPrice = oldPrice - priceDeduction;
this.setState({ totalPrice: newPrice, ingredients: updatedIngredients });
this.updatePurchaseState(updatedIngredients);
};
purchaseHandler = () => {
this.setState({ purchasing: true });
};
purchaseCancelHandler = () => {
this.setState({ purchasing: false });
};
purchaseContinueHandler = () => {
alert("You continue!");
};
render() {
const disabledInfo = {
...this.state.ingredients,
};
for (let key in disabledInfo) {
disabledInfo[key] = disabledInfo[key] <= 0;
}
// {salad: true, meat: false, ...}
return (
<Aux>
<Modal show={this.state.purchasing} modalClosed={this.purchaseCancelHandler}>
<OrderSummary
ingredients={this.state.ingredients}
price={this.state.totalPrice}
purchaseCancelled={this.purchaseCancelHandler}
purchaseContinued={this.purchaseContinueHandler}
/>
</Modal>
<Burger ingredients={this.state.ingredients} />
<BuildControls
ingredientAdded={this.addIngredientHandler}
ingredientRemoved={this.removeIngredientHandler}
disabled={disabledInfo}
purchasable={this.state.purchasable}
ordered={this.purchaseHandler}
price={this.state.totalPrice}
/>
</Aux>
);
}
}
export default BurgerBuilder;
提前致谢
1 回答
在我看来,这种情况的合适数据结构将是一个简单的 array (参见here)
您可以在数组的每个索引位置维护成分的类型,因此当用户想要添加/删除成分时,他可以在弹出窗口中输入位置 .
如果您需要任何其他说明,请发表评论 .