Hey all, I've been trying to work with TCA NavigationStackStore and I think I may be doing something wrong. I have a root view that can navigate to 2 different child views. It looks like this:
struct RootDomain: Reducer {
struct Path: Reducer {
enum State {
case child1(Child1Domain.State)
case child2(Child2Domain.State)
}
enum Action {
case child1(Child1Domain.Action)
case child2(Child2Domain.Action)
}
var body: some ReducerOf<Self> {
Scope(state: /State.child1, action: /Action.child1) {
Child1Domain()
}
Scope(state: /State.child2, action: /Action.child2) {
Child2Domain()
}
}
}
struct State {
var path = StackState<Path.State>()
}
enum Action {
case path(StackAction<Path.State, Path.Action>)
}
var body: some ReducerOf<Self> {
Reduce { state, action in
return .none
}
.forEach(\.path, action: /Action.path) {
Path()
}
}
}
struct RootView: View {
var store: StoreOf<RootDomain> = .init(initialState: .init(), reducer: { RootDomain() })
var body: some View {
NavigationStackStore(self.store.scope(state: \.path, action: { .path($0) })) {
NavigationLink(state: RootDomain.Path.State.child1(.init())) {
Text("Root")
}
} destination: { store in
switch store {
case .child1:
CaseLet(
/RootDomain.Path.State.child1,
action: RootDomain.Path.Action.child1,
then: Child1View.init(store:)
)
case .child2:
CaseLet(
/RootDomain.Path.State.child2,
action: RootDomain.Path.Action.child2,
then: Child2View.init(store:)
)
}
}
}
}
When clicking the NavigationLink it will push Child1View onto the stack. Child1 looks like this:
struct Child1Domain: Reducer {
struct State: Equatable {
var child2State = Child2Domain.State()
}
enum Action: Equatable {}
var body: some ReducerOf<Self> {
Reduce { state, action in
return .none
}
}
}
struct Child1View: View {
var store: StoreOf<Child1Domain>
var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
NavigationLink(state: RootDomain.Path.State.child2(viewStore.child2State)) {
Text("Child 1")
}
Text("Child 2 was tapped: \(viewStore.child2State.wasTapped.description)")
}
}
}
When the link is clicked in Child1View, Child2 is then pushed onto the stack. I want to keep track of the state changes in Child2 from Child1. Here is Child2:
struct Child2Domain: Reducer {
@Dependency(\.dismiss) var dismiss
struct State: Equatable {
var wasTapped = false
}
enum Action: Equatable {
case buttonTapped
}
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .buttonTapped:
state.wasTapped = true
return .run { _ in await self.dismiss() }
}
}
}
}
struct Child2View: View {
var store: StoreOf<Child2Domain>
var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
Button("Child 2") {
viewStore.send(.buttonTapped)
}
}
}
}
How can I get access to Child2 state and actions from Child1 if the path is located in the root, and navigation is done by NavigationLinks?