Skip to content

@BindableState shared between sibling States #1676

Answered by mbrandonw
patch-benjamin asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @patch-benjamin, the approach mentioned by @ibrahimkteish above can work. It's a little bit of boilerplate to maintain the computed property, but it does work. We also have a case study to show this style off.

There are a few other approaches.

First off, if we look at the vanilla SwiftUI example you shared:

@State var username = ""
var body: some View {
  VStack {
    EditUsernameView($username)
    FancyEditUserNameView($username)
  }
}

If one of the examples in your app really is this simple, then you should know you can still use bindings to have a single source of truth but hand off little slices of that source of truth:

@ObservedObject var viewStore: ViewStoreOf<Feature>
var body: s…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@patch-benjamin
Comment options

Comment options

You must be logged in to vote
2 replies
@patch-benjamin
Comment options

@patch-benjamin
Comment options

Answer selected by patch-benjamin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants