我试着在react中制作我的第一个应用程序,一切正常,但我试着计算账单金额和人数,并将账单除以人数,然后添加小费.我知道对某些人来说,这可能是5分钟的解决方案,但我昨天就开始了这方面的工作,仍然找不到解决方案.

如果有人 Select 小费,我想把账单金额除以personAmount+小费.我做错了什么

import React, { useState } from 'react'
import style from './BillSplitter.module.css'

const tips = [5, 10, 15, 25]



const Input = ({ label, id, handleChange, name, type, placeholder }) => (
  <>
    <label className={`${style.label}`} htmlFor={id}>{label}</label>
    <input className={`${style.input}`} type={type || "number"} id={id} name={name || id} placeholder={placeholder} onChange={handleChange}></input>
    <br />
  </>
);

const SelectTip = ({ tip }) => {
  <>
    <button className='tipButton' value={tip}></button>
  </>
}

function BillSplitter() {
  const [tipAmount, setTipAmount] = useState(0)
  const [billAmount, setBillAmount] = useState(0)
  const [personAmount, setPersonAmount] = useState(0)


  function handleChange(e) {
    console.log(e.target.value)
  }



  return (
    <div className={`${style.wrapper}`}>
      <div className={`${style.box}`}>
        <div className={`${style.top}`}>
          <h2 className={`${style.title}`}>Bill Splitter</h2>
          <p className={`${style.personBillAmount}`}>Person Bill Amount</p>
          <p onChange={handleChange} className={`${style.totalPersonAmount}`} >$ {tipAmount}</p>
        </div>
        <div className={`${style.bottom}`}>

          <Input handleChange={handleChange} className={`${style.clases}`} placeholder='Amount of bill' label="Bill value">{billAmount}</Input>

          <Input handleChange={handleChange} className={`${style.clases}`} placeholder='Number of people' label="Number of people">{personAmount}</Input>

          <div className={`${style.tipBox}`}>
            <p>Select tip</p>
            {tips.map((tip) => {
              return <button className={`${style.tipButton}`}>{tip} %</button>
            })}
          </div>
        </div>
      </div>
    </div>


  )
}


export default BillSplitter

推荐答案

Input组件添加了onChange个处理程序,并传递不同的输入setter函数.然后在 Select 尖端时进行计算.

try 以下操作:

const tips = [5, 10, 15, 25];

const Input = ({ label, id, handleChange, name, type, placeholder }) => (
  <React.Fragment>
    <label htmlFor={id}>{label}</label>
    <input
      type={type || "number"}
      id={id}
      name={name || id}
      placeholder={placeholder}
      onChange={(e) => handleChange(e.target.value)}
    ></input>
    <br />
  </React.Fragment>
);

function BillSplitter() {
  const [billAmount, setBillAmount] = React.useState(0);
  const [personAmount, setPersonAmount] = React.useState(0);
  const [perCost, setPerCost] = React.useState(0);

  const calculatePerPeronAmount = (tip) => {
    if (personAmount > 0) {
      setPerCost(((1 + 0.01 * tip) * billAmount) / personAmount);
    }
  };

  return (
    <div>
      <div>
        <div>
          <h2>Bill Splitter</h2>
          <p>Person Bill Amount</p>
          <p>$ {perCost}</p>
        </div>
        <div>
          <Input
            handleChange={setBillAmount}
            placeholder="Amount of bill"
            label="Bill value"
          >
            {billAmount}
          </Input>

          <Input
            handleChange={setPersonAmount}
            placeholder="Number of people"
            label="Number of people"
          >
            {personAmount}
          </Input>

          <div>
            <p>Select tip</p>
            {tips.map((tip) => {
              return (
                <button onClick={() => calculatePerPeronAmount(tip)} key={tip}>
                  {tip} %
                </button>
              );
            })}
          </div>
        </div>
      </div>
    </div>
  );
}

ReactDOM.render(<BillSplitter />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

Javascript相关问答推荐

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

提交表格后保留Web表格中的收件箱值?

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

在服务器上放置了Create Reaction App Build之后的空白页面

为什么Mutations 观察器用微任务队列而不是macrotask队列处理?

使用GraphQL查询Uniswap的ETH价格

Html文件和客户端存储的相关问题,有没有可能?

是什么导致了这种奇怪的水平间距错误(?)当通过JavaScript将列表项元素追加到无序列表时,是否在按钮之间?

在Matter.js中添加从点A到另一个约束的约束

为什么当我更新数据库时,我的所有组件都重新呈现?

Phaserjs-创建带有层纹理的精灵层以自定义外观

JavaScript:如果字符串不是A或B,则

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

在没有任何悬停或其他触发的情况下连续交换图像

ComponentWillReceiveProps仍在React 18.2.0中工作

谷歌饼图3D切片

表单数据中未定义的数组键

JavaScript:多个图像错误处理程序

更新文本区域行号

如何在加载页面时使锚定标记成为焦点