Navigation: Problem of understanding ViewStore Binding value #866
-
Hello everyone! In the last days we did a deep dive and tried to analyse the TicTacToe app out of the examples, but we couldn't figure out the particular behaviour when tapping the "Log in" button with 2FA credentials entered: In the app, there is a (optional) navigation from the login screen to the 2FA-screen. We understood that the IfLetStore only gives us a view as destination if the twoFactor attribute of the LoginState is != nil. We also understood that the view is presented while the isActive bool is true which depends on the isTwoFactorActive attribute of the ViewState, which again depends on the twoFactor attribute of the LoginState. What we don't understand is how the value in the send closure of the binding can be true without having sent the loginButtonTapped action in the first place. The send closure indicates that the loginButtonTapped action can only be executed, if the twoFactors attribute is != nil, but the twoFactors attribute can only be != nil if the loginButtonTapped action was executed. Can somebody please explain this black magic to us? 😄 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The Binding(
get: { viewStore.isTwoFactorActive },
set: { isActive in
if isActive {
viewStore.send(.loginButtonTapped)
} else {
viewStore.send(.twoFactorDismissed)
}
}
) So the value in So the order is:
|
Beta Was this translation helpful? Give feedback.
The
send
part ofViewStore.binding
is theset
part of a vanilla SwiftUI binding, which gives the view the ability to influence the value in a source of truth the binding was derived from. We could create a binding from scratch usingBinding.init
and it would look like this:So the value in
set
is sent from the view in order to influence th…