struct LoginView: View {
@State var isUserLoggedIn = false
NavigationStack{
//.....
Button {
Task{
await viewModel.doLogin(email: email, password: password)
}
} label: {
Text(Constants.LOGIN)
.foregroundColor(Constants.BACKGROUND_COLOR)
.font(Font.custom(Constants.FREDOKA_MEDIUM, size: 25))
.frame(maxWidth: .infinity)
}
}.onChange(of: isUserLoggedIn) { isUserLoggedIn in
debugPrint(newPasswordValue)
}
}
I am not able to understand how to write NavigationLink
if a state changes, there is no push method in navigation stack as well
2
Answers
...to do programmatic navigation in swiftui using navigationstack once state changes...
,try this approach using
NavigationPath
andnavigationDestination
To add on to workingdog’s answer, navigationDestination’s Type can only be used for one path. If you try to start a new navigationDestination route using Int, you may end up going back to an empty view or get errors.
Also if you ever need to access the router from different views, you could create an observableObject Router class: