reactjs - 单击时删除数组中的元素并且只接受输入标签中的数字类型?[反应JS]
问题描述
我是前端新手,现在仍在学习这方面的东西。我有两个需要你帮助的斗争,但首先我需要展示代码:
import React, {useState, useRef, useMemo} from 'react'
function Content() {
const [name, setName] = useState('')
const [price, setPrice] = useState('')
const [products, setProducts] = useState([])
const ref = useRef()
const handleSubmit = () => {
setProducts([...products, {
name,
price: Number(price)
}])
setName('')
setPrice('')
ref.current.focus()
}
const total = useMemo(() => {
console.log('Calculating..')
const calculator = products.reduce((init, currentValue) => {
return init + currentValue.price
}, 0)
return calculator;
}, [products])
return (
<div style={{padding: '10px 32px'}}>
<input
ref={ref}
value={name}
placeholder={'Enter name..'}
onChange={e => setName(e.target.value)}
/>
<br />
<input
value={price}
placeholder={'Enter price..'}
onChange={e => setPrice(e.target.value)}
/>
<br />
<button
children={'Add'}
onClick={handleSubmit}
/>
<h3>Total :{total}</h3>
<ul>
{products.map((product, index) => (
<li key={index}>
{product.name} - {product.price}
<span className={'delete'} data-index={index}> đồng x</span>
</li>
))}
</ul>
</div>
)
}
export default Content;
所以我正在做一个小应用程序所谓的计算器,在第二个输入标签中,我不知道如何让它只接受数字,以及如何压入跨度标签,特别是按'x',它必须被移除。
请大家帮帮我,谢谢大家!
解决方案
推荐阅读
- python-3.x - Python中str类型的find()和index(),哪个时间复杂度更好?
- powershell - PowerShell:带有 PID 的 AppActivate 不起作用
- php - 将变量从 HTML 表单传递到 PHP
- javascript - 在 react/javascript 中修改一个 zip 文件
- python - 模块在 Django 中没有属性
- javascript - h1 元素文本没有使用 javascript 更改,但控制台中的更改
- python - Python 与 Spyder
- html - 我只想在我的 html 页面中插入图片?
- javascript - 有条件的 onclick 函数在没有点击的情况下触发?
- sql - SQL Server - 即使不满足条件,也会显示 LEFT 表中的所有行