Replies: 3 comments 2 replies
-
Since the parent usually has knowledge of its children actions and state, you can handle the children actions from the parent reducer and update the parent state accordingly. struct RootState: Equatable {
var text = ""
var detailState: DetailState?
}
enum RootAction: Equatable {
case showDetail
case hideDetail
case detailAction(DetailAction)
}
let rootReducer = Reducer<RootState, RootAction, Void>.combine(
detailReducer.optional().pullback(
state: \.detailState,
action: /RootAction.detailAction,
environment: { _ in }
),
Reducer<RootState, RootAction, Void> { state, action, _ in
switch action {
case .showDetail:
state.detailState = .init()
return .none
case .hideDetail:
state.detailState = nil
return .none
case .detailAction(.someChildAction):
// Mutate the parent state or even the child state
// state.text = "Hello"
// state.detailState?.doStuff = true
return .none
}
}
)
struct DetailState: Equatable {
var doStuff = false
}
enum DetailAction: Equatable {
case someChildAction
}
let detailReducer = Reducer<DetailState, DetailAction, Void> { state, action, _ in
switch action {
case .someChildAction:
// Do nothing or mutate some local state
// state.doStuff = true
return .none
}
} I believe since the Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
I'm kinda new here with the concept of TCA, but could you create a generic parent I do not know how exactly the |
Beta Was this translation helpful? Give feedback.
-
This is very inspiring thread! Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
I am building an app with TCA and I keep coming across a pattern and I’m not sure I am handling it in the best way.
The pattern is that I have a
RootView
with some state, and I want to display another view (lets call itDetailView
) to edit a subset of theRootView
state.DetailView
will depend on some mutable state fromRootView
plus some local state that it can use when editing.What I have been doing in this case is separating the mutable state from
RootView
from the (optional)DetailView.LocalState
. The presentation of DetailView is dependent on theDetailView.LocalState
.I have used this pattern so much that I defined a generic struct that I could use as the
DetailView
’s state for its Store.This solution works, but there is a lot of overhead (enough that makes me think I am missing some easier way to handle it).
Here is a simple example of a
RootView
that passes a String to aDetailView
, which can update that String based on the state of some local Bools.Is there a more concise way of doing this that I am missing?
Beta Was this translation helpful? Give feedback.
All reactions