1 回答

TA貢獻2019條經驗 獲得超9個贊
我無法測試它,但對你來說是一個好的開始,我使用 ES6 語法......
import React, { useState } from 'react';
import { Hamburger, TotalPrice, ItemList } from './SuperComponents.jsx';
const App = () => {
? ? const [ingredients, setIngredients] = useState([]);
? ? // You are not using this state
? ? // const [totalPrice, setTotalPrice] = useState(0);
? ? const addIngredients = (product) => {
? ? ? ? setIngredients([...ingredients, { ...product, displayId: Math.random() }]);
? ? };
? ? const removeIngredients = (product) => {
? ? ? ? const selectedProduct = ingredients.find(
? ? ? ? ? ? (ingredient) => ingredient.name === product.name
? ? ? ? );
? ? ? ? const { targetId } = selectedProduct;
? ? ? ? setIngredients(
? ? ? ? ? ? ingredients.filter((ingredient) => ingredient.displayId !== targetId)
? ? ? ? );
? ? };
? ? const calculateTotal = () => {
? ? ? ? let total = 4;
? ? ? ? ingredients.forEach((item) => (total += item.price));
? ? ? ? return total.toFixed(2);
? ? };
? ? return (
? ? ? ? <>
? ? ? ? ? ? <Hamburger ingredients={ingredients} />
? ? ? ? ? ? <TotalPrice total={() => calculateTotal()} />
? ? ? ? ? ? <ItemList
? ? ? ? ? ? ? ? items={ingredients}
? ? ? ? ? ? ? ? addIngredients={(i) => addIngredients(i)}
? ? ? ? ? ? ? ? removeIngredients={(i) => removeIngredients(i)}
? ? ? ? ? ? ? ? selectedIngredients={ingredients}
? ? ? ? ? ? />
? ? ? ? </>
? ? );
};
export default App;
添加回答
舉報