Default send parameter in viewStore.binding

Let's say we want to put a NavigationLink inside a view.

Struct ExampleView: View {
let store:  Store<ExampleState, ExampleAction>

var body: some View {
WithViewStore( { viewStore in
NavigationLink(destination: AnotherView(), isActive: viewStore.binding(get: {viewStore.booleanValue}, send: [A] )) {

The send parameter in viewStore.binding (denoted as [A]) is meant to send some action. However, even for cases where I don't need to send any action I have to fill [A] in the above code to build the app. If I leave [A] empty or just put an empty closure, it's giving me error. To avoid this, I have to create an action that doesn't do anything just to put it as a placeholder.

How can I make the app run without putting an empty action in the send parameter when I don't have any action to send?

By the way, I'm really enjoying learning TCA so far :) Thanks a lot!

Hi Yoonseok, :wave:

the binding passed to the NavigationLink is used to reset the viewStore.booleanValue whenever the pushed someOtherView view is no longer active.

TCA follows unidirectional data flow and other than SwiftUI does not allow views to directly mutate the state. Instead, actions are used at all times to mutate the state. Due to this constraint, instead of implementing an empty action, you should pass an action that sets viewStore.booleanValue to false. If you were to ignore the sent action, the first state mutation after popping someOtherView leads to SwiftUI assuming, based on the given State exposed through the ViewStore, that the NavigationLink is active and therefore show someOtherView again.

I hope that helps. If you have any follow up questions, feel free to ask them. :slight_smile:

1 Like

Hello Daniel,

Thanks for your reply! Now it's crystal clear. Have a nice day :)

Terms of Service

Privacy Policy

Cookie Policy