SwitchStore in Preview results in multiple screens #2260
-
DescriptionHi! So I was working with the I tried with a Vanilla SwiftUI I believe that this behaviour is a direct result of Checklist
Expected behaviorSeeing only 3 preview contents (3 because I have explicitly added 3 previews to my preview provider) Actual behaviorI see N preview contents (N being 3 times the number Steps to reproduceUsing the following sample, just check the XCode's live preview: import ComposableArchitecture
import SwiftUI
struct TestReducer: ReducerProtocol {
enum State: Equatable {
case content(String)
case other(String)
case nothing
}
enum Action: Equatable {}
var body: some ReducerProtocolOf<Self> {
EmptyReducer()
}
}
struct TestView: View {
let store: StoreOf<TestReducer>
var body: some View {
SwitchStore(store) { _ in
CaseLet(
state: /TestReducer.State.content,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Content")
}
CaseLet(
state: /TestReducer.State.other,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Other")
}
CaseLet(
state: /TestReducer.State.nothing,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Nothing")
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView(store: .init(
initialState: .content("Hello, world!"),
reducer: TestReducer()
))
.previewDisplayName("Content")
TestView(store: .init(
initialState: .other("Hello, other world!"),
reducer: TestReducer()
))
.previewDisplayName("Other")
TestView(store: .init(
initialState: .nothing,
reducer: TestReducer()
))
.previewDisplayName("Loading")
}
} The Composable Architecture version information0.54.0 Destination operating systemiOS 15 and later Xcode version informationVersion 14.3.1 (14E300c) Swift Compiler version informationswift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
-SwitchStore(store) { _ in
+SwitchStore(store) {
+ switch $0 {
+ case .content:
CaseLet(
state: /TestReducer.State.content,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Content")
}
+ case .other:
CaseLet(
state: /TestReducer.State.other,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Other")
}
+ case .nothing:
CaseLet(
state: /TestReducer.State.nothing,
action: { (action: TestReducer.Action) in action }
) { store in
Text("Nothing")
}
+ }
} I believe this will get previews to work more as you expect. Also note that CaseLet(/TestReducer.State.content, /* ... */) Because this isn't a bug in the library I'm going to convert this to a discussion. |
Beta Was this translation helpful? Give feedback.
SwitchStore
expects you to explicitly switch over the initial state that is passed along:I…