Skip to content

Instantly share code, notes, and snippets.

@felixscheinost
Created May 27, 2020 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixscheinost/0a5fef0988e6db928726188161b7f680 to your computer and use it in GitHub Desktop.
Save felixscheinost/0a5fef0988e6db928726188161b7f680 to your computer and use it in GitHub Desktop.
import SwiftUI
import ComposableArchitecture
struct DebugNavigationLinkView: View {
struct ViewState: Equatable {
var selectedNumber: Int?
}
enum ViewAction {
case selectNumber(Int?)
}
static var reducer = Reducer<ViewState, ViewAction, Void> { state, action, environment in
switch action {
case .selectNumber(let number):
state.selectedNumber = number
return .none
}
}
var store: Store<ViewState, ViewAction>
var body: some View {
NavigationView {
WithViewStore(store) { viewStore in
List {
ForEach(1...4, id: \.self) { number in
NavigationLink(
destination: IfLetStore(
self.store.scope(state: { $0.selectedNumber }).actionless,
then: DebugNavigationLinkChildView.init(store:),
else: Text("selectedNumber is nil!")
),
tag: number,
selection: viewStore.binding(get: { $0.selectedNumber }, send: {
print("NavigationLink for \(number) is sending \($0)")
return ViewAction.selectNumber($0)
})
) {
Text("Go to \(number)")
}
}
}
}
}
}
}
struct DebugNavigationLinkChildView: View {
let store: Store<Int, Never>
var body: some View {
WithViewStore(store) { viewStore in
Text("I am \(viewStore.state)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment