@Binding bools within a VStack getting conflated

I am running into an issue where two distinct bool bindings are both being toggled when I toggle only one of them. My component looks like

VStack {
     Checkbox(label: "Checkbox 1", isOn: $stateVar1)
     Checkbox(label: "Checkbox 2", isOn: $stateVar2)
}

where my CheckBox component looks like

struct Checkbox: View {
    let label: String
    @Binding var isOn: Bool

    var body: some View {
        Button {
            isOn.toggle()
        } label: {
            HStack {
                Image(systemName: isOn ? "checkmark.square" : "square")
                Text(label)
            }
            .foregroundStyle(.black)
        }
    }
}

If I click on one checkbox, both of them get toggled. However, if I simply remove the checkboxes from the VStack, then I am able to toggle them both independently. I believe this is a bug with bool Bindings, but if anyone can point out why I am mistaken that would be much appreciated :)

Hi @premalpatel, I've created a test project with your Checkbox view and am unable to reproduce this behavior. Can you provide some more information about your environment, including the OS version and Xcode version?

Based on your sample code, I'm assuming you have something like

@State var stateVar1 = false
@State var stateVar2 = false

declared in the View that contains the checkboxes. Are these values conflated anywhere else in your project?

Best,

J

@Binding bools within a VStack getting conflated
 
 
Q