const [product, setProduct] = useState({
productName: "",
quantity: 0,
basePrice: 0,
sellPrice: 0,
})
const handleChange = (e) => {
let fieldName = e.target.id;
let fieldValue = e.target.value;
setProduct({
fieldName: fieldValue
});
};
Code above doesnt seem to work. when i log fieldName however, it return productName
.
I tried to explicitly use productName instead and its working
const handleChange = (e) => {
let fieldName = e.target.id;
let fieldValue = e.target.value;
setProduct({
productName: fieldValue
});
}
Can anyone tell me whats going on under the hood why the initial approach is not working?
PS reason i want to use e.target.id is because there are multiple fields that needs updating Thank you
question from:
https://stackoverflow.com/questions/65650026/react-setstate-not-working-using-event-property 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…