I have observed object which is created in view and I want to have function that will occur when the object bool property is changed. I want something similar to .onTapGesture. Is there a way to do it? have function where the body of the function is created outside of that object?
Asked
Active
Viewed 2,282 times
2 Answers
8
Let's replicate...
class Demo: ObservableObject {
@Published var value = false
}
struct DemoView: View {
@ObservedObject var demo = Demo()
var body: some View {
Text("Demo")
.onReceive(demo.$value) { flag in
// call here your function
}
}
}

Asperi
- 228,894
- 20
- 464
- 690
0
use @ObservedObject to make change in value an use @StateObject to read the published value

Admiral
- 1
- 3