I have a checkbox and when it's checked/unchecked, it writes true or false in the console, however, when I try and save the value, it's always passing false.
html:
<input type="checkbox"
name="Status"
value="Status"
onChange={(e) => {carStatus(e)}) />
const CarState = {
carId: 0,
Make: "",
Model: ""
Active: "" //this is a bit in the DB
}
const [carMake, setCarMake] = useState(CarState);
const [carModel, setCarModel] = useState(CarState);
const [carActive, setActive] = useState(CarState);
const carStatus = (e) => {
const status = e.target.checked;
if(status)
{
console.log("checked " + status);
setCarActive({ ...carActive, status});
}
else
{
console.log("checked" + status);
setCarActive({ ...carActive, status});
}
const saveCar = () => {
var data = {
make: carMake.CarMake,
model: carModel.Model,
active: saleActive.Active //always false even if checkbox is checked
};
//passes data to the API
}
Is there a better way to do this or a different way? I can see true/false in F12 if the checkbox is checked or not, however, just passes false when I click the save button [saveCar function]