React-onChange函数中的更新状态(useState)问题

问题描述 投票:0回答:1

我在更新表单的handleItemChange函数上的状态时遇到问题。你有解决方案吗 ?非常感谢...

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

const addPrestation = () => {

    const id = Date.now().toString();
    const prestation = {...items};
    prestation[id] = {
        customer:customer,
        id: id,
        delivery: "",
        quantity: "",
        unit: "",
        unitPrice: "",
        tva: "",
        htAmount: "",
        ttcAmount: ""
    }
    setItems([...items, {prestation:prestation}])
}

 const handleItemChange = (event, prestation, field ) => {
   const value = event.target.value;
   const clonePresta = {...prestation};
   clonePresta[field] = value;
   const clonePrestations = {...items};
   clonePrestations[clonePresta.id] = clonePresta;

   setItems(???)
}
reactjs use-state
1个回答
0
投票

我认为您应该这样做:

const handleItemChange = (event, prestation, field ) => {
   const value = event.target.value;
   const clonePresta = {...prestation};
   clonePresta[field] = value;
   const clonePrestations = {...items};
   clonePrestations[clonePresta.id] = clonePresta;

   setItems([...items, {presentation: clonePresta})
}
© www.soinside.com 2019 - 2024. All rights reserved.