onChange(of:) is not getting triggered
Why does onChange(of: authManager.authState) in View1 not fire when I change authState in View2?
struct View1: View {
@Environment(AuthManager.self) var authManager
var body: some View {
NavigationStack {
VStack {
Text("\(authManager.authState)")
}
.onChange(of: authManager.authState) {
print("Changed")
}
}
}
}
struct View2: View {
@Environment(AuthManager.self) var authManager
var body: some View {
NavigationStack {
VStack {
Text("\(authManager.authState)")
.onTapGesture {
if authManager.authState == .signedIn {
authManager.authState = .signedOut
} else {
authManager.authState = .signedIn
}
}
}
}
}
}
@Obersvable class AuthManager {
var authState: AuthState = .signedout
...
}
struct ContentView: View {
@State private var authManager = AuthManager()
var body: some View {
MainTabView()
.environment(authManager)
}
}