SwiftUI Navigation link doesn't work when filtering ForEach

Viewed 529

I have a segmented picker to filter the list rows inside a ForEach but the NavigationLinks stop working when filtering, they have the tap animation but never change view. I am using CoreData for the deck. When I tap the rows in Standard they work normally, then I tap Wild and the NavigationLinks still work but then when I go back to standard this is what happens:

enter image description here

This is the code I have:

 ForEach(decks.filter{$0.format == formats[selectedFormat]}, id: \.self) { deck in
                    HStack {
                        ZStack {
                            DeckRow(deck: deck)
                            NavigationLink(destination: DeckView()) {
                                EmptyView()
                            }
                        }
                    }
                }
2 Answers

The provided code is not testable, but by reading assume the reason of issue is in equal navigation links, try the following (that will result in updating links on filtering)

ZStack {
    DeckRow(deck: deck)
    NavigationLink(destination: DeckView()) {
        EmptyView()
    }.id(deck)        // << here !!
}

or, as it would be expected, make that DeckView dependent on deck, like DeckView(model: deck), which also make each navigation link unique and refreshable on filter.

you have to give "deck" also to your deckview so it can show the right deckview

Related