0
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
  • You'r sure, you want a navigation for a NavigationStack or a NavigationSplitView and not a sheet for example? Also, the doLogin() what's it doing anyway? It seems odd when you are calling this from your view code. It should be rather somewhere in your application network code. Commented Aug 31, 2023 at 11:58
  • Does this answer your question? Update environment object on listRow click before navigation Commented Aug 31, 2023 at 12:01

2 Answers 2

3
+50

...to do programmatic navigation in swiftui using navigationstack once state changes..., try this approach using NavigationPath and navigationDestination

struct LoginView: View {
    
    @State var isUserLoggedIn = false
    @State var path = NavigationPath()   // <-- here
    
    var body: some View {
        NavigationStack(path: $path) {   // <-- here
            Button {
                Task {
                    await viewModel.doLogin(email: email, password: password)
                    if isUserLoggedIn {
                        path.append(1)  // <-- here
                    }
                }
            } label: {
                Text("Login")
                    .foregroundColor(Constants.BACKGROUND_COLOR)
                    .font(Font.custom(Constants.FREDOKA_MEDIUM, size: 25))
                    .frame(maxWidth: .infinity)
            }
            .navigationDestination(for: Int.self) { _ in     // <-- here
                NextView()
            }
        }
    }
}

struct NextView: View {
    
    var body: some View {
        Text("NextView")
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

wait few hours and i will give u the bounty as well
0

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:

final class Router: ObservableObject {
    @Published var path: NavigationPath = .init() 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.