ForEach scope reducers with different types. #3236
vadimkrutovlv
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. 👋
I wanted to ask for an advice on how to scope reducers with a different types and preserve data order.
Suppose I'm working on potential feature Survey this feature has multiple question types such as list of choices, free text, numeric answer and date answer. Since all question types are different from each other and might have different requirements on how to handle answers for me it makes sense each question to have its own reducer. So my initial idea was to use something what is suggested here because by end of the day survey is nothing but list with different row types, but I can't figure out what would be the best approach to hold on multiple state types and actions. I hope I did explain myself clear enough.
Here's a code example to represent better what I'm trying to achieve.
Survey model:
Reducer
SwiftUI View:
Update
After spending some time learning how the Destination and Path reducers work I was able to achieve what I want, here is the code, in case anyone has better ideas I'd be very happy to hear them. In the example we have some sort of List feature where list items of different types are arranged in a specific order.
Reducers:
Here's an actual reducer which does the thing, it's very similar to what @Reducer macro generates for Path and Destination reducers with small addition to it which is State conformance to
Identifiable
, this is needed to satisfy requirements ofIdentifiedArrayOf
type.I think an alternative approach might be to update the @Reducer macro by adding new "identifiable" option for the state parameter.
@Reducer(state: .identifiable)
.Small extension which enables store.case syntax in the view similarly as path and destination reducers.
And finally SwiftUI views
If you try this code you will see that order in which reducers are scoped is exactly the same as we populated items property of type
IdentifiedArrayOf<ListItems.State>
in ListItemsFeature, which is ListToggleFeature -> ListFreeTextFeature -> ListToggleFeature.Beta Was this translation helpful? Give feedback.
All reactions