SwiftUI – how to avoid navigation hardcoded into the view?

The closure is all you need!

struct ItemsView<Destination: View>: View {
    let items: [Item]
    let buildDestination: (Item) -> Destination

    var body: some View {
        NavigationView {
            List(items) { item in
                NavigationLink(destination: self.buildDestination(item)) {
                    Text(item.id.uuidString)
                }
            }
        }
    }
}

I wrote a post about replacing the delegate pattern in SwiftUI with closures.
https://swiftwithmajid.com/2019/11/06/the-power-of-closures-in-swiftui/

Leave a Comment