Skip to content

How to best isolate spinning pickers from view updates #1072

Answered by iampatbrown
ryanbooker asked this question in Q&A
Discussion options

You must be logged in to vote

Hey Ryan,

This is a cool example showing how scoping can be used to better control view updates :)

but I'd like to know if there is a better way

Depending on your needs, one option could be to extract the Picker to a helper function

func picker<Content: View>(
  _ title: String, store: Store<Int, Int>, @ViewBuilder content: @escaping () -> Content
) -> some View {
  WithViewStore(store) { viewStore in
    Picker(title, selection: viewStore.binding(send: { $0 }), content: content)
  }
}

Then instead of scoping to \.$a and .binding you could scope directly to state: \.a, action: { .set(\.$a, $0) }. Resulting in the following:

picker("A", store: store.scope(state: \.a, action: { .set(\.$a, $0

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@ryanbooker
Comment options

Comment options

You must be logged in to vote
2 replies
@ryanbooker
Comment options

@iampatbrown
Comment options

Answer selected by ryanbooker
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants