Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.onChange(of: ) for multiple @State properties at once?

Is there a way I can use .onChange to detect the change of multiple @State properties at once? I know I could just chain 2 .onChange modifiers but it would be better if I could just detect all at once and run some code.

@State private var width = 0.0
@State private var height = 0.0

var body: some View {
    Button(action: {
        width += 0.1
    }, label: {
        Text("Width + 0.1")
    })
    .onChange(of: width) { _ in 
        print("Changed")
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For this case here is the simplest I think

.onChange(of: width + height) { _ in
    print("Changed")
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading