NavigationLink isActive deprecated. How to convert to new api?

Hello, I am having trouble converting using the new api for iOS16. I tried searching the internet for solutions but no luck as of today. Can someone please help me with this.

NavigationLink( destination: ChatsView(), isActive: $showChatView, label: { })

I would really appreciate any tips on how to proceed. Thanks a bunch!

Hi Albert,

You have a few options here.

Option 1

Here is some demo code on how to use NavigationStack and navigationDestination without isActive.

This presents a simple list of two items, one that says Arriving, one that says Leaving. Clicking on them will navigate you to another page that says Hello or Goodbye.

This code uses NavigationLink(, value) where you can fill in the value parameter with something (here, a String), and then in the .navigationDestination(for:), you will navigate to a destination view and pass in that specific value. So .navigationDestination(for: String.self) matches the String type that was passed to the value of the NavigationLink. Then, that String will be passed to the next view accordingly. As commented, you can also use NavigationLink(, destination:) to immediately go to a destination instead.

struct SayingsDetail: View{
    var sayings: String
    var body: some View {
        Text(sayings)
    }
}


struct ContentView: View {

    var body: some View {
        NavigationStack {
            List {
                NavigationLink("Arriving", value: "hello")
                NavigationLink("Leaving", value: "goodbye")
        
               /*
                Here, I've used NavigationLink(_, value:)
                which presents a view that corresponds to a values (which is "hello" here, meaning the "Arriving" Navigation will happen when "hello" is in the above array.
                
                Another option is to use   NavigationLink(_, destination:)
                which presents a destination view when you click on the link
                */
            }
            .navigationTitle("Common Sayings")
            .navigationDestination(for: String.self) { sayings in
                SayingsDetail(sayings: sayings)
                /* Here, there are a few ways you can navigate to the expected page. depending on the value passed in here, you can navigate to where you want to go after moving to SayingsDetail
               
                 */
            }
        
        }
    }
}

Option 2

Another option, which may be more like isActive is to use .navigationDestination(isPresented:, destination:). This allows you to have a @State variable for the value of "isPresented" (much like "isActive") and then toggle that state in a Button. Then, you can navigate to the destination based on this.

struct Option2: View {
    @State private var isPresented = false
 var body: some View {
        NavigationStack() {
            List {
                Button("hello"){
                    isPresented = true
                }
 .navigationDestination(isPresented: $isPresented){
                SayingsDetail(sayings: "hi")
            }
}
}

To learn more about the new Navigation APIs, please visit
https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types or watch the following WWDC session https://developer.apple.com/videos/play/wwdc2022/10054/

NavigationLink isActive deprecated. How to convert to new api?
 
 
Q