Custom back button in the NavigateAndLoad case study

I'm struggling to figure out how to add a custom back button to a view, while following the NavigateAndLoad case study example.

when I add
@Environment(\.presentationMode) var mode
to the CounterView, the IfLetStore breaks in the parent view.

NavigationLink(
              destination: IfLetStore(
                self.store.scope(
                  state: \.selection?.value,
                  action: LoadThenNavigateListAction.counter
                ),
                then: CounterView.init(store:mode:)
              ),
              tag: row.id,
              selection: viewStore.binding(
                get: \.selection?.id,
                send: LoadThenNavigateListAction.setNavigation(selection:)
              )
            )

The error is:
Cannot convert value of type '(Store<CounterState, CounterAction>, Environment<Binding>) -> CounterView' to expected argument type '(Store<CounterState, CounterAction>) -> CounterView'

Can anyone guide me on how to rewrite the IfLetStore statement, so that it can initialize the CounterView while it includes an environment variable?

When you add the environment value presentationMode to a view it becomes apart of the initializer, but you are not meant to actually pass it along. It has a default value.

So this means you must explicitly open the closure for the then view in order to pass along just the store:

then: { CounterView(store: $0) }

Amazing, thank you so much!

Alternatively, you should just mark the environment property as private (it should be private anyway) and it shouldn’t become part of the initialiser.

2 Likes