所以這是我第一次嘗試使用 React。我必須計算預訂的(總)價格。保留價格由以下幾個因素決定:船長:每米 1.25 歐元人數:每人1歐元電費:每天 1.25 歐元假設船長 10 米,船上有 2 個人用電。他們待了 2 天。計算如下:船長 (10) * 1.25 * 2 = 25人數(2)* 1 * 2 = 4使用電力(真)= 1.25 * 2 = 2.5總價 = 25 歐元 + 4 歐元 + 2.5 歐元 = 21.5 歐元我嘗試在我的代碼中實現它,它看起來像這樣:import React, { Component } from 'react'import BoatForm from "./BoatForm";import StayForm from "./StayForm";export default class PriceCalculator extends Component { /** * So, we created a form for the customers so that they can register their boat. * Now we want to let them know how much their reservation costs. * The size of the boat, amount of people, amount of days and use of electricity are all parameters. */ constructor(props) { super(props) this.state = { boat_length: '', amount_of_people: '', arrival_date: '', leave_date: '', use_of_electricity: '', box_number: '' } } /** * We use date fields in our form to define the stay date of the customer. * That means we have to calculate the days ourselves. */ calculateStayTime() { const ArrivalDate = new Date(this.state.arrival_date); const LeaveDate = new Date(this.state.leave_date); // calculate difference const DiffTime = Math.abs(LeaveDate - ArrivalDate); const DiffDays = Math.ceil(DiffTime / (1000 * 60 * 60 * 24)); let AmountOfDays = DiffDays; return AmountOfDays; }}我努力將變量從 calculateStayTime() 傳遞到 calculatePrice()。任何人都可以幫助我如何將我的計算公式化為 React.JS?
在 React 中計算總價
Cats萌萌
2023-05-11 10:11:25