How to manage ForEachStore with NavigationLink's binding?

Hi @Viranchee!

Do you plan on driving navigation from state programmatically? E.g. deep-linking? If not, you should be able to use the bindingless NavigationLink APIs and let SwiftUI handle it for you:

NavigationLink(
  destination: Text(viewStore.value.givenName)
) {
  EmptyView() // label here
}

If you do want to drive navigation with state programmatically, it's a little trickier. We have a couple examples of lists of navigation links in the demos that ship with the repo (example 1, example 2), but they may be slightly more complicated in that they are working with optional sub-state for the row that is selected.

If you can get away with not using the binding-based NavigationLink API, though, I'd recommend it. SwiftUI currently has a few bugs around programmatic navigation that can make it quite tricky:

  • iPhone-based navigation needs to apply the .navigationViewStyle(StackNavigationViewStyle()) modifier to the navigation view in order to get the right binding behavior when drilling multiple layers into a navigation stack.
  • It's not currently possible to deep link several layers into a navigation stack all at once. Instead you need the UI to tick once between each layer, which is also pretty tricky to manage.

We're hoping that these things get fixed in the upcoming betas.

4 Likes