ForEachStore with a placeholder Loading #1525
Replies: 1 comment 1 reply
-
Hey @knight6700. You can probably project your collection's count into some struct State {
var isLoading: Bool
var items: IdentifiedArrayOf<Item.State>
} You can probably do like this in your view: struct ViewState: Equatable {
let isLoading: Bool
let collectionIsEmpty: Bool
init(_ state: State) {
self.isLoading = state.isLoading
self.itemsIsEmpty = items.isEmpty
}
}
var body: some View {
WithViewStore(store, observe: ViewState.init) { viewStore in
if viewStore.isLoading {
IsLoadingPlaceholder()
} else {
if viewStore.itemsIsEmpty {
NoItemsPlaceholder()
} else {
List {
ForEachStore(store.scope(state: \.items,…) { … }
}
}
}
}
} Or you can use an var body: some View {
List {
ForEachStore(store.scope(state: \.items,…) { … }
}.overlay {
WithViewStore(store, observe: ViewState.init) { viewStore in
if viewStore.isLoading {
IsLoadingPlaceholder()
} else if viewStore.itemsIsEmpty {
NoItemsPlaceholder()
}
}
}
} It really depends on your layout and the kind of placeholder you want to display, but this is then mostly a SwiftUI problem. You can also model your collection status using an enum instead of two booleans if you want to avoid "invalid" states like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
how can i implement a placeholder with ForEachStore in ListView
Beta Was this translation helpful? Give feedback.
All reactions