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

React - Problem with update state (useState) in onChange function

我在更新表单的 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(???)
}

我认为你应该这样做:

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})
}
const handlePrestaChange = ({currentTarget}) => {
    const {name, value, id} = currentTarget;
    const clonePresta = {...prestation}
    clonePresta[name] = value;
    setPrestation({...prestation, [name]: value })
    const tabItems = [...items];
    tabItems[id] = clonePresta;
    setItems([...tabItems])
}