-
It's sample app. If I tap navigation link, MainFeatures's and you already know that struct MainFeature: ReducerProtocol {
struct State: Equatable {
var navigationIsActive = false
var subState: SubFeature.State?
}
enum Action: Equatable {
case setNavigation(Bool)
case subAction(SubFeature.Action)
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .setNavigation(true):
state.subState = SubFeature.State()
state.navigationIsActive = true
case .setNavigation(false):
state.subState = nil
state.navigationIsActive = false
case .subAction:
break
}
return .none
}
.ifLet(\.subState, action: /MainFeature.Action.subAction) {
SubFeature()
}
}
}
struct MainView: View {
var store: StoreOf<MainFeature>
var body: some View {
WithViewStore(store, observe: { $0 }) { viewStore in
NavigationStack {
NavigationLink("count", isActive: viewStore.binding(get: \.navigationIsActive, send: MainFeature.Action.setNavigation)) {
IfLetStore(store.scope(state: \.subState, action: MainFeature.Action.subAction)) {
SubView(store: $0)
}
}
.navigationTitle("Main")
}
}
}
}
struct SubFeature: ReducerProtocol {
struct State: Equatable {
var count = 0
}
enum Action: Equatable {
case decrementButtonTapped
case incrementButtonTapped
}
func reduce(into state: inout State, action: Action) -> Effect<Action, Never> {
switch action {
case .decrementButtonTapped:
state.count -= 1
case .incrementButtonTapped:
state.count += 1
}
return .none
}
}
struct SubView: View {
var store: StoreOf<SubFeature>
var body: some View {
WithViewStore(store, observe: { $0 }) { viewStore in
HStack {
Button("-", action: { viewStore.send(.decrementButtonTapped) })
Text("\(viewStore.count)")
Button("+", action: { viewStore.send(.incrementButtonTapped) })
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
ghost
Oct 14, 2022
Replies: 1 comment 1 reply
-
sorry. solved... |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sorry. solved...
change
NavigationStack
->NavigationView
.