亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 React.js 項目中將類組件轉換為功能組件?

如何在 React.js 項目中將類組件轉換為功能組件?

慕田峪7331174 2023-07-06 15:29:23
在我觀看的項目中,他們使用類組件,但我想使用鉤子使用功能組件來執行這些操作。你們能如何幫助我?我嘗試了很多次,但無法完成這個翻譯。我還在努力我的代碼(導入的數據是“成分”):class App extends React.Component {  constructor(props) {    super(props)    this.state = {      ingredients: [],      totalPrice: 0    }    this.addIngredients = this.addIngredients.bind(this)    this.removeIngredients = this.removeIngredients.bind(this)    this.calculateTotal = this.calculateTotal.bind(this)  }  addIngredients(product) {    this.setState({      ingredients: [...this.state.ingredients].concat([        { ...product, displayId: Math.random() }      ])    })  }  removeIngredients(product) {    const selectedProduct = this.state.ingredients.find((ingredient) => {      return ingredient.name === product.name    })    const targetId = selectedProduct.displayId    this.setState({      ingredients: this.state.ingredients.filter((ingredient) => {        return ingredient.displayId !== targetId      })    })  }  calculateTotal() {    let total = 4    this.state.ingredients.forEach((item) => {      total += item.price    })    return total.toFixed(2)  }  render() {    return (      <div>        <Hamburger ingredients={this.state.ingredients} />        <TotalPrice total={this.calculateTotal} />        <ItemList          items={ingrediends}          addIngredients={this.addIngredients}          removeIngredients={this.removeIngredients}          selectedIngredients={this.state.ingredients}        />      </div>    )  }}export default App
查看完整描述

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;


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號