Skip to main content
A store represents the runtime that powers the application. It is the object that you will pass around to views that need to interact with the application.

Class Definition

Initialization

initializer
Initializes a store from an initial state and a reducer.Parameters:
  • initialState: The state to start the application in
  • reducer: The reducer that powers the business logic of the application
  • prepareDependencies: A closure that can be used to override dependencies that will be accessed by the reducer

Usage Example

You will typically construct a single store at the root of your application:

Methods

Sending Actions

(Action) -> StoreTask
Sends an action to the store.This method returns a StoreTask, which represents the lifecycle of the effect started from sending an action. You can use this value to tie the effect’s lifecycle and cancellation to an asynchronous context, such as SwiftUI’s task view modifier:
Returns: A StoreTask that represents the lifecycle of the effect executed when sending the action.
(Action, Animation?) -> StoreTask
Sends an action to the store with a given animation.Parameters:
  • action: An action
  • animation: An animation
(Action, Transaction) -> StoreTask
Sends an action to the store with a given transaction.Parameters:
  • action: An action
  • transaction: A transaction

Scoping

<ChildState, ChildAction>(KeyPath<State, ChildState>, CaseKeyPath<Action, ChildAction>) -> Store<ChildState, ChildAction>
Scopes the store to one that exposes child state and actions.This can be useful for deriving new stores to hand to child views in an application:
Scoping in this fashion allows you to better modularize your application.Parameters:
  • state: A key path from State to ChildState
  • action: A case key path from Action to ChildAction
Returns: A new store with its domain (state and action) transformed.

State Access

<R>((_ state: State) -> R) -> R
Calls the given closure with a snapshot of the current state of the store.A lightweight way of accessing store state when state is not observable and state property is unavailable.Parameters:
  • body: A closure that takes the current state of the store as its sole argument. If the closure has a return value, that value is also used as the return value of the withState method.
Returns: The return value, if any, of the body closure.

Properties

StorePublisher<State>
A publisher that emits when state changes.This publisher supports dynamic member lookup so that you can pluck out a specific field in the state:

Scoping Example

For a tab view application with multiple tabs:

Type Aliases

typealias
A convenience type alias for referring to a store of a given reducer’s domain.
Instead of specifying two generics:
You can specify a single generic:

Supporting Types

StoreTask

The type returned from Store.send(_:) that represents the lifecycle of the effect started from sending an action. Methods:
  • cancel(): Cancels the underlying task
  • finish(): Waits for the task to finish
  • isCancelled: A Boolean value that indicates whether the task should stop executing
Example:

StorePublisher

A publisher of store state that supports dynamic member lookup for plucking specific fields.

ObservableObject Conformance

The store conforms to ObservableObject but is not observable via the @ObservedObject property wrapper. This conformance is completely inert and its sole purpose is to allow stores to be held in SwiftUI’s @StateObject property wrapper. Instead, stores should be observed through Swift’s Observation framework (or the Perception package when targeting iOS <17) by applying the @ObservableState() macro to your feature’s state.