微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何增加或减少数组的多个输入的值?

如何解决如何增加或减少数组的多个输入的值?

就这样:

const [count,setCount] = useState(1 as any)

{items.map((item: any) => (...

然后将数组中的项目映射到购物车中。我希望能够增加或减少数量

<input
 type="number"
 min="1"
 value={count}
 onChange={(event): any => {
 setCount(event.target.value)
 }}
/>

它将更改所有项目的值。但是我想独立更改元素的值。

解决方法

尝试这种方式

const [items,setItems] = useState(items); // items array []

const onTextChanged = (index,value) => {
    const items = [...items]; 
    items[index].count = value; <-- "count" is example key for showing way out of this -->
    setItems(items);
}

{items.map((item,index) => (

 <input
  type="number"
  min="1"
  value={item.count || 0}
  onChange={(event): any => {
    onTextChanged(index,event.target.value)
  }}
 />

)}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。