-
Architecture: App module have a main feature that contains Module 1: App that imports NetworkUI, xUI, yUI, zUI. Main feature: import ComposableArchitecture
import SwiftUI
import NetworkUI
import xUI
import yUI
import zUI
@Reducer
public struct ModuleList {
@ObservableState
public struct State: Equatable {
var path = StackState<Path.State>()
public init(path: StackState<Path.State> = StackState<Path.State>()) {
self.path = path
}
}
public enum Action {
case path(StackAction<Path.State, Path.Action>)
}
public var body: some Reducer<State, Action> {
...
.forEach(\.path, action: \.path) {
Path()
}
}
public init() {}
@Reducer
public struct Path {
@ObservableState
public enum State: Equatable {
case network(NetworkPath.State = .list())
// case x(XPath.State)
// case y(YPath.State)
// case z(ZPath.State)
}
public enum Action {
case network(NetworkPath.Action)
// case x(XPath.Action)
// case y(YPath.Action)
// case z(ZPath.Action)
}
public var body: some Reducer<State, Action> {
Scope(state: \.network, action: \.network) {
NetworkPath()
}
...
}
}
}
@Reducer
public struct NetworkPath {
@ObservableState
public enum State: Equatable {
case list(NetworkList.State = .init())
case detail(NetworkDetail.State)
}
public enum Action {
case list(NetworkList.Action)
case detail(NetworkDetail.Action)
}
public var body: some Reducer<State, Action> {
Scope(state: \.list, action: \.list) {
NetworkList()
}
Scope(state: \.detail, action: \.detail) {
NetworkDetail()
}
}
}
... NetworkUI feature: import ComposableArchitecture
import SwiftUI
import NetworkKit
struct NetworkListView: View {
@Bindable var store: StoreOf<NetworkList>
var body: some View {
List {
ForEach(store.entities, id: \.id) { entity in
NavigationLink(state: ModuleList.Path.State.network(.detail(.init(entity: entity)))) {
NetworkListCellView(store: Store(initialState: NetworkListCell.State(entity: entity)) {
NetworkListCell()
})
}
}
}
.listStyle(.plain)
.navigationTitle("Network")
}
} The problem is in order to use How can i decouple NavigationStack and child features? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @wynioux, you have two options for decoupling the child features. First, and the easiest, is to simply not use It's also worth noting that The second option is to pull |
Beta Was this translation helpful? Give feedback.
Hi @wynioux, you have two options for decoupling the child features.
First, and the easiest, is to simply not use
NavigationLink(state:)
. As you can see it necessarily means that the child feature must be able to compilePath.State
, which means it must compile every child feature'sState
. Instead you can use aButton
that sends an action, and then a parent domain listens for that actions and appends to thepath
. This is what we do the SyncUps demo app provided in the repo (see here).It's also worth noting that
NavigationLink(state:)
only really works with the most basic kinds of navigation. You are not allowed to perform any logic when the link is tapped. All that can happen is that the …