docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Store<TStoreState>

    A store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it. Your application should only have a single store in a Redux app. As your app grows, instead of adding stores, you split the root reducer into smaller reducers independently operating on the different parts of the state tree.

    The store has the following responsibilities:
    - Holds application state
    - Allows access to state via GetState()
    - Allows state to be updated via Dispatch(IAction)
    - Registers listeners via Subscribe<TSelected>(Selector<TStoreState, TSelected>, Listener<TSelected>, SubscribeOptions<TSelected>)
    - Handles unregistering of listeners via the function returned by Subscribe<TSelected>(Selector<TStoreState, TSelected>, Listener<TSelected>, SubscribeOptions<TSelected>)

    Here are some important principles you should understand about Reducers:
    - Reducers are the only way to update the state.
    - Reducers are pure functions that take the previous state and an action, and return the next state.
    - Reducers must be pure functions. They should not mutate the state, perform side effects like API calls or routing transitions, or call non-pure functions.
    - Reducers must not do asynchronous logic.
    - Reducers must not call other Reducer<TState>.
    - Reducers must not call Subscribe<TSelected>(Selector<TStoreState, TSelected>, Listener<TSelected>, SubscribeOptions<TSelected>).
    - Reducers must not call GetState()
    - Reducers must not call Dispatch(IAction)

    Inheritance
    object
    Store<TStoreState>
    Implements
    IStore<TStoreState>
    IStore
    IDispatchable
    IDisposable
    IStateProvider<TStoreState>
    INotifiable<TStoreState>
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.AppUI.Redux
    Assembly: Unity.AppUI.Redux.dll
    Syntax
    [Preserve]
    public class Store<TStoreState> : IStore<TStoreState>, IStore, IDispatchable, IDisposable, IStateProvider<TStoreState>, INotifiable<TStoreState>
    Type Parameters
    Name Description
    TStoreState

    The type of the store state.

    Constructors

    Store(Reducer<TStoreState>, TStoreState)

    Creates a Redux store that holds the complete state tree of your app.

    Declaration
    [Preserve]
    public Store(Reducer<TStoreState> reducer, TStoreState initialState)
    Parameters
    Type Name Description
    Reducer<TStoreState> reducer

    The root reducer that returns the next state tree.

    TStoreState initialState

    The initial state of the store.

    Remarks

    You should not use directly this constructor to create a store. Instead, use Store.CreateStore to create a store with optional enhancers. This constructor should be called only by enhancers to return an enhanced store.

    Fields

    m_Reducer

    The reducer of the store.

    Declaration
    protected readonly Reducer<TStoreState> m_Reducer
    Field Value
    Type Description
    Reducer<TStoreState>

    m_State

    The state of the store.

    Declaration
    protected TStoreState m_State
    Field Value
    Type Description
    TStoreState

    m_SubscribeLock

    The subscribe lock of the store.

    Declaration
    protected readonly object m_SubscribeLock
    Field Value
    Type Description
    object

    m_Subscriptions

    The subscriptions of the store.

    Declaration
    protected readonly List<ISubscription<TStoreState>> m_Subscriptions
    Field Value
    Type Description
    List<ISubscription<TStoreState>>

    Properties

    dispatcher

    The dispatcher of the store.

    Declaration
    public Dispatcher dispatcher { get; set; }
    Property Value
    Type Description
    Dispatcher

    reducer

    The reducer of the store.

    Declaration
    public Reducer<TStoreState> reducer { get; }
    Property Value
    Type Description
    Reducer<TStoreState>

    Methods

    Dispatch(IAction)

    Dispatches an action. This is the only way to trigger a state change.

    Declaration
    public void Dispatch(IAction action)
    Parameters
    Type Name Description
    IAction action

    An object describing the change that makes up the action.

    Exceptions
    Type Condition
    ArgumentException

    Thrown if the reducer for the action type does not exist.

    ArgumentNullException

    Thrown if the action is null.

    Dispose()

    Declaration
    public void Dispose()

    GetState()

    Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.

    Declaration
    public TStoreState GetState()
    Returns
    Type Description
    TStoreState

    The current state tree of your application.

    NotifySubscribers()

    Notify the subscribers of a new state.

    Declaration
    public void NotifySubscribers()

    Subscribe<TSelected>(Selector<TStoreState, TSelected>, Listener<TSelected>, SubscribeOptions<TSelected>)

    Subscribe to a state change and listen to a specific part of the state.

    Declaration
    public IDisposableSubscription Subscribe<TSelected>(Selector<TStoreState, TSelected> selector, Listener<TSelected> listener, SubscribeOptions<TSelected> options = default)
    Parameters
    Type Name Description
    Selector<TStoreState, TSelected> selector

    The selector to get the selected part of the state.

    Listener<TSelected> listener

    The listener to be invoked on every dispatch.

    SubscribeOptions<TSelected> options

    The options for the subscription.

    Returns
    Type Description
    IDisposableSubscription

    A Subscription object that can be disposed.

    Type Parameters
    Name Description
    TSelected

    The type of the selected part of the state.

    Exceptions
    Type Condition
    ArgumentNullException

    Thrown if the selector or listener is null.

    Implements

    IStore<TState>
    IStore
    IDispatchable
    IDisposable
    IStateProvider<TState>
    INotifiable<TState>

    Extension Methods

    StoreExtensions.Dispatch(IDispatchable, string)
    StoreExtensions.Dispatch(IDispatchable, ActionCreator)
    StoreExtensions.DispatchAsyncThunkCoroutine<TPayload, TArg>(IDispatchable, AsyncThunkAction<TPayload, TArg>, CancellationToken)
    StoreExtensions.DispatchAsyncThunk<TPayload, TArg>(IDispatchable, AsyncThunkAction<TPayload, TArg>, CancellationToken)
    StoreExtensions.Dispatch<TPayload>(IDispatchable, string, TPayload)
    StoreExtensions.Dispatch<TPayload>(IDispatchable, ActionCreator<TPayload>, TPayload)
    StoreExtensions.Subscribe<TStoreState>(INotifiable<TStoreState>, Listener<TStoreState>, SubscribeOptions<TStoreState>)
    StoreExtensions.GetState<TState, TSelected>(IStateProvider<TState>, Selector<TState, TSelected>)
    InstrumentedStoreExtensions.AsInstrumentedStore<TState>(IStore<TState>)
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)