Conforming to ReducerProtocol: body vs reduce(into:) #1830
-
In the reducer protocol migration guide it is shown that leaf node reducers should conform to Would there be any downside to always using the result builder approach even for leaf-node features? A leaf node reducer would just contain a single |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Hi @RandomActsOfCode, nope no real downside! If that's the style that you/your team prefer, then definitely go for it! There's only one small caveat, and it's something that even came up recently. Using |
Beta Was this translation helpful? Give feedback.
-
Hey @RandomActsOfCode! I'm personally always using |
Beta Was this translation helpful? Give feedback.
-
@tgrapperon @mbrandonw thank you both for confirming 🚀. Looking forward to the migration! |
Beta Was this translation helpful? Give feedback.
-
I've been using this pattern for new features. I like keeping the core reducer and composed reducers separate, Or, it's easy to modify into the optimal version – by either removing 'core', or renaming 'core' to 'reduce' and removing 'body' public var body: some ReducerProtocol<State, Action> {
Reduce(self.core)
}
func core(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
...
}
} |
Beta Was this translation helpful? Give feedback.
Hi @RandomActsOfCode, nope no real downside! If that's the style that you/your team prefer, then definitely go for it!
There's only one small caveat, and it's something that even came up recently. Using
Reduce { ... }
in a body can give the Swift compiler troubles when it comes to autocomplete and warnings. So, just supply the generics if you go that route,Reduce<State, Action> { ... }
, and the compiler should be able to do a better job. Hopefully this gets fixed in Swift someday in the future.