CombineReducers and Pullbacks on 1.0 ? #2343
Replies: 3 comments 1 reply
-
Hi @JeromeTonnelierOgury, I believe the tutorial is more up-to-date than the videos. Did you give it a try already? Cf. https://pointfreeco.github.io/swift-composable-architecture/main/tutorials/composablearchitecture/02-01-yourfirstpresentation |
Beta Was this translation helpful? Give feedback.
-
In the modern syntax, that code would look something like the following: import ComposableArchitecture
struct CounterReducer: Reducer {
struct State: Equatable {
var count = 0
var favorites: Set<Int> = []
}
enum Action {
case incrementButtonTapped
case decrementButtonTapped
case saveButtonTapped
case removeButtonTapped
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .incrementButtonTapped:
state.count += 1
return .none
case .decrementButtonTapped:
state.count -= 1
return .none
case .saveButtonTapped:
state.favorites.insert(state.count)
return .none
case .removeButtonTapped:
state.favorites.remove(state.count)
return .none
}
}
}
}
struct ProfileReducer: Reducer {
struct State: Equatable {
var favorites: Set<Int> = []
}
enum Action {
case removeButtonTapped(Int)
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case let .removeButtonTapped(number):
state.favorites.remove(number)
return .none
}
}
}
}
struct AppReducer: Reducer {
struct State: Equatable {
var count = 0
var favorites: Set<Int> = []
var counter: CounterReducer.State {
get {
.init(count: self.count, favorites: self.favorites)
}
set {
self.count = newValue.count
self.favorites = newValue.favorites
}
}
var profile: ProfileReducer.State {
get { .init(favorites: self.favorites) }
set { self.favorites = newValue.favorites }
}
}
enum Action {
case counter(CounterReducer.Action)
case profile(ProfileReducer.Action)
}
var body: some ReducerOf<Self> {
Scope(state: \.counter, action: /Action.counter) {
CounterReducer()
}
Scope(state: \.profile, action: /Action.profile) {
ProfileReducer()
}
}
} Regular, every day reducer combining is achieved just by the builder block given to you by the |
Beta Was this translation helpful? Give feedback.
-
Thanks to all of you for your insights :-) var body: some ReducerOf<Self> {
Scope(state: \.counter, action: /Action.counter) {
CounterReducer()
}
Scope(state: \.profile, action: /Action.profile) {
ProfileReducer()
}
} It seems that there is no more pullbacks and combine, juste a Scope operator in the body, that's magic <3 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone :-)
We are still waiting for our company to buy us a subscription to PointFree, but in the meantime, I wanted to give TCA a try.
Basically I want to implement Parent/Child behavior, so I watched the episode #147 about Derived Behavior, but it seems that a lot has changed since then and 1.0.
Reducer.combine does not exists anymore (I guess it's CombineReducers now), but I can't make it work.
To practice, I took the code from episode #146 and tried to implement the TCA version of the counter view. All is good up to the point where I have to create the global reducer and combine 2 child reducers : I can't create Counter or Profile reducers, and I can't merge them :-/
If someone has an updated code or some hints on how to achieve that, I'm all ears!
Thanks for your help, and thanks for such an amazing architecture :-)
Beta Was this translation helpful? Give feedback.
All reactions