IfLetStore - mbrandonw/swift-composable-architecture GitHub Wiki

IfLetStore

A view that safely unwraps a store of optional state in order to show one of two views.

public struct IfLetStore<State, Action, Content>: View where Content: View

When the underlying state is non-nil, the then closure will be performed with a Store that holds onto non-optional state, and otherwise the else closure will be performed.

This is useful for deciding between two views to show depending on an optional piece of state:

IfLetStore(
  store.scope(state: \SearchState.results, action: SearchAction.results),
  then: SearchResultsView.init(store:),
  else: Text("Loading search results...")
)

And for performing navigation when a piece of state becomes non-nil:

 NavigationLink(
   destination: IfLetStore(
     self.store.scope(state: \.detail, action: AppAction.detail),
     then: DetailView.init(store:)
   ),
   isActive: viewStore.binding(
     get: \.isGameActive,
     send: { $0 ? .startButtonTapped : .detailDismissed }
   )
 ) {
   Text("Start!")
 }

Inheritance

View

Initializers

init(_:then:else:)

Initializes an IfLetStore view that computes content depending on if a store of optional state is nil or non-nil.

public init<IfContent, ElseContent>(_ store: Store<State?, Action>, then ifContent: @escaping (Store<State, Action>) -> IfContent, else elseContent: @escaping @autoclosure () -> ElseContent) where Content == _ConditionalContent<IfContent, ElseContent>

Parameters

  • store: - store: A store of optional state.
  • ifContent: - ifContent: A function that is given a store of non-optional state and returns a view that is visible only when the optional state is non-nil.
  • elseContent: - elseContent: A view that is only visible when the optional state is nil.

init(_:then:)

Initializes an IfLetStore view that computes content depending on if a store of optional state is nil or non-nil.

public init<IfContent>(_ store: Store<State?, Action>, then ifContent: @escaping (Store<State, Action>) -> IfContent) where Content == IfContent?

Parameters

  • store: - store: A store of optional state.
  • ifContent: - ifContent: A function that is given a store of non-optional state and returns a view that is visible only when the optional state is non-nil.

Properties

content

let content: (ViewStore<State?, Action>) -> Content

store

let store: Store<State?, Action>

body

var body: some View
⚠️ **GitHub.com Fallback** ⚠️