【发布时间】:2021-01-12 09:08:32
【问题描述】:
我想要的输出是这样的
{'3afdrjke3j43kjkjf' : 3 , '8fsdfdsni3ni3dsfa' :2 }
我试过这样但是没用
const [productQuantity, setProductQuantity] = useState([]);
const handleQuantity = (e, id) => {
setProductQuantity({id : e.target.value}) // this outputs {id : "3"}
}
每当我触发 onchange 事件时,id 和数量都会传递给 handleQuantity 函数。我想将键值对连接到状态。
<Form.Control onChange={(e) => handleQuantity(e, cartProduct._id)} as="select">
{
[...Array(cartProduct.quantity)].map((_, index) =>{
return <option>{index + 1}</option>
})
}
</Form.Control>
在此先感谢 :)
【问题讨论】:
-
所以您想在每次触发 setProductQuantity 时追加新条目?可以试试 setProductQuantity(productQuantity.concat([{id : e.target.value}]))
-
谢谢!它正在连接到状态,但“id”应该是这样的“34784asfdsaf”而不是它返回 {id :'2'}
标签: javascript arrays reactjs react-native