Tuesday, November 28, 2023
HomeIOS Developmentios - Why onChange shouldn't be working for baby view attributes

ios – Why onChange shouldn’t be working for baby view attributes


I’ve quite simple two views. ContentView and ChildView.
I wish to observe baby view VM attributes in ContentView VIA .onChange

class ContentViewVM: ObservableObject {
    @Printed var childVM = ChildVM()
}

struct ContentView: View {
    
    @StateObject var contentVM = ContentViewVM()
    
    var physique: some View {
        ChildView(viewModel: contentVM.childVM)
            .onChange(of: contentVM.childVM.isPaymentPresented) { newValue in
                    print("Change Noticed")
            }            
    }
}

struct ChildView: View {
    @StateObject var viewModel: ChildVM
    var physique: some View {
        Button("Deep Stage Set off") {
            viewModel.isPaymentPresented.toggle()
            print("Deep Stage Triggered: (viewModel.isPaymentPresented)")
        }
    }
}

class ChildVM: ObservableObject {
    @Printed var isPaymentPresented: Bool = false
}

After I click on, ChildView Button.

‘Deep Stage Triggered: true’ is printed however ‘Change Noticed’ shouldn’t be printing.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments