View

extension View
  • Fires when a child view dispatches an action.

    Declaration

    Swift

    @inlinable
    public func onAction(perform: @escaping OnActionViewModifier.ActionModifier) -> some View

    Parameters

    perform

    Calls the closure when an action is dispatched. An optional new action can be returned to change the action.

    Return Value

    The modified view.

  • Sends the provided action when the view appears.

    Declaration

    Swift

    @inlinable
    public func onAppear(dispatch action: Action) -> some View

    Parameters

    action

    An action to dispatch every time the view appears.

    Return Value

    The modified view.

  • Sends the provided action plan when the view appears.

    In the follow example an ActionPlan is created that automatically updates a list of todos when the filter property of the TodoList state changes. All the view needs to do is dispatch the action when it appears.

    // In the TodoListAction file:
    
    enum TodoListAction: Action {
      case setTodos([TodoItem])
      case setFilterBy(String)
    }
    
    extension TodoListAction {
    
      static func queryTodos(from services: Services) -> Action {
        ActionPlan<AppState> { store in
          store.didChange
            .filter { $0 is TodoListAction }
            .map { _ in store.state?.todoList.filterBy ?? "" }
            .removeDuplicates()
            .flatMap { filter in
              services
                .queryTodos(filter: filter)
                .catch { _ in Just<[TodoItem]>([]) }
                .map { todos -> Action in TodoListAction.setTodos(todos) }
            }
        }
      }
    }
    
    // In a SwiftUI View:
    
    @Environment(\.services) private var services
    @MappedState private var todos: [TodoItem]
    
    var body: some View {
      Group {
        renderTodos(todos: todos)
      }
      .onAppear(dispatch: TodoListAction.queryTodos(from: services))
    }
    

    Declaration

    Swift

    @inlinable
    public func onAppear<T>(dispatch actionPlan: ActionPlan<T>, cancelOnDisappear: Bool = true) -> some View

    Parameters

    actionPlan

    An action to dispatch every time the view appears.

    cancelOnDisappear

    It will cancel any subscription from the action when the view disappears. If false, it keeps the subscription alive and reppearances of the view will not re-call the action.

    Return Value

    The modified view.

  • Injects a store into the environment.

    The store can then be used by the @EnvironmentObject property wrapper. This method also enables the use of View.mapState(updateOn:_:) to map substates to a view.

    struct RootView: View {
      // Passed in from the AppDelegate or SceneDelegate class.
      var store: Store<AppState>
    
    
      var body: some View {
        RootAppNavigation()
          .provideStore(store)
      }
    
    }
    

    Declaration

    Swift

    @inlinable
    public func provideStore<State>(_ store: Store<State>) -> some View where State : Decodable, State : Encodable, State : Equatable

    Parameters

    store

    The store object to inject.

    Return Value

    The modified view.