'How does the onChange-onIonChange event works for IonCheckBox when toggling?

I'm trying to save a checkbox value as a boolean depending on either the user checked or not. Right now it's like this:

let checkbox1: boolean= false;

<IonCheckbox className={"checkbox-options"} color="primary" onIonChange={(e) => {

    checkbox1 = !checkbox1;
   
    console.log('testing checkbox1: ' + checkbox1);
                                                   
}}></IonCheckbox>

The value updates only in the first toggle, from false to true. But if I check again, the value remains true instead of toggling to false. Can I do this using the onIonChange event or should I be using other property?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source