'How POST all selected checked ARRAY item in Axios Post in reactjs
I have to a checkbox which is checked and the item must Axios post to get the total amount of the checked item, the checked item I have saved in the checked array, but I don't know how to call the checked array and post the checked array in the Axios.
This is the checked box input that I used to save the data in a checked array using the e.target.value.
<input
className="border-2"
type="checkbox"
value={`${items.ProductID},${items.VariationID},${items.Count}`}
defaultChecked={!!checked[items.VariationID]}
onChange={(e) => {
handleChange(
e,
items.ProductID,
items.VariationID,
items.Count
);
}}
/>
This is how I create the handlechange to save the checked array and post the checked array. in the post, it replaces the prev item with the current item, but it should have both items in the post, I don't know how to call the checked array and post the checked array AXIOS Post.
const handleChange = async (e, itemId, vId, qty) => {
let updatedList = [...checked];
if (e.target.checked) {
updatedList = [...checked, e.target.value];
} else {
updatedList.splice(checked.indexOf(e.target.value), 1);
}
setChecked(updatedList);
if (checked) {
await Axios.post("/user/OrderSummary", {
Products: [
{
ProductID: itemId,
VariationID: vId,
Count: qty,
},
],
CouponCode: [],
});
}
console.log(updatedList);
};
Solution 1:[1]
await Axios.post("/user/OrderSummary", {
Products: [
{
ProductID: itemId,
VariationID: vId,
Count: qty,
},
],
CouponCode: [],
})
.then(response => response.data)
.then(data => {
// Data
})
.catch(error => {
// Error
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kennarddh |