Created
March 13, 2023 23:47
-
-
Save darrarski/ebf09c06c206bb52ef7ddd55adfdab6c to your computer and use it in GitHub Desktop.
TCA prerelease 1.0 NavigationLinkStore returningLastNonNilValue issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ComposableArchitecture | |
import SwiftUI | |
struct Parent: Reducer { | |
struct State: Equatable { | |
@PresentationState var child: Child.State? | |
} | |
enum Action: Equatable { | |
case presentChildButtonTapped | |
case dismissChildButtonTapped | |
case child(PresentationAction<Child.Action>) | |
} | |
var body: some ReducerOf<Self> { | |
Reduce { state, action in | |
switch action { | |
case .presentChildButtonTapped: | |
state.child = Child.State() | |
return .none | |
case .dismissChildButtonTapped: | |
state.child = nil | |
return .none | |
case .child(_): | |
return .none | |
} | |
} | |
.ifLet(\.$child, action: /Action.child) { | |
Child() | |
} | |
} | |
} | |
struct Child: Reducer { | |
struct State: Equatable {} | |
enum Action: Equatable { | |
case dismissButtonTapped | |
} | |
@Dependency(\.dismiss) var dismiss | |
var body: some ReducerOf<Self> { | |
Reduce { state, action in | |
switch action { | |
case .dismissButtonTapped: | |
return .fireAndForget { await dismiss() } | |
} | |
} | |
} | |
} | |
struct ParentView: View { | |
let store: StoreOf<Parent> | |
var body: some View { | |
VStack { | |
WithViewStore(store, observe: { $0.child != nil }) { viewStore in | |
Text(viewStore.state ? "presented" : "dismissed") | |
} | |
NavigationLinkStore( | |
store: store.scope(state: \.$child, action: Parent.Action.child), | |
onTap: { ViewStore(store.stateless).send(.presentChildButtonTapped) }, | |
destination: ChildView.init | |
) { | |
Text("Present child") | |
} | |
Button("Dismiss child") { | |
ViewStore(store.stateless).send(.dismissChildButtonTapped) | |
} | |
} | |
.buttonStyle(.borderedProminent) | |
.navigationTitle("Parent") | |
} | |
} | |
struct ChildView: View { | |
let store: StoreOf<Child> | |
var body: some View { | |
Button("Dismiss") { | |
ViewStore(store.stateless).send(.dismissButtonTapped) | |
} | |
.buttonStyle(.borderedProminent) | |
.navigationTitle("Child") | |
} | |
} | |
struct ExampleView: View { | |
var body: some View { | |
NavigationView { | |
ParentView(store: Store( | |
initialState: Parent.State(), | |
reducer: Parent() | |
)) | |
Text("Placeholder") | |
} | |
.navigationViewStyle(.automatic) | |
} | |
} | |
struct ExampleView_Previews: PreviewProvider { | |
static var previews: some View { | |
ExampleView() | |
} | |
} | |
@main | |
struct ExampleApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ExampleView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment