Class Store
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<TState>(string)
- Allows state to be updated via Dispatch(Action)
- Registers listeners via Subscribe<TState>(string, Action<TState>)
- Handles unregistering of listeners via the function returned by Subscribe<TState>(string, Action<TState>)
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.
- Reducers must not call Subscribe<TState>(string, Action<TState>).
- Reducers must not call GetState<TState>(string)
- Reducers must not call Dispatch(Action)
Inherited Members
Namespace: Unity.AppUI.Redux
Assembly: Unity.AppUI.Redux.dll
Syntax
public class Store
Constructors
Store()
Creates a Redux store that holds the complete state tree of your app.
Declaration
public Store()
Methods
CombineReducers(params Reducer[])
Create a reducer that combines multiple reducers into one.
Declaration
public static Reducer CombineReducers(params Reducer[] reducers)
Parameters
Type | Name | Description |
---|---|---|
Reducer[] | reducers | The reducers to combine. |
Returns
Type | Description |
---|---|
Reducer | A reducer that combines the given reducers. |
CreateAction(string)
Create a new Action. See Action for more information.
Declaration
public static ActionCreator CreateAction(string type)
Parameters
Type | Name | Description |
---|---|---|
string | type | The type of the action. |
Returns
Type | Description |
---|---|
ActionCreator | A new Action. |
CreateAction(string, Type)
Create a new Action. See Action<TPayload> for more information.
Declaration
public static ActionCreator CreateAction(string type, Type actionType)
Parameters
Type | Name | Description |
---|---|---|
string | type | The type of the action. |
Type | actionType | The type of the action to instantiate. |
Returns
Type | Description |
---|---|
ActionCreator | A new Action. |
CreateAction<TPayload>(string)
Create a new Action. See Action<TPayload> for more information.
Declaration
public static ActionCreator<TPayload> CreateAction<TPayload>(string type)
Parameters
Type | Name | Description |
---|---|---|
string | type | The type of the action. |
Returns
Type | Description |
---|---|
ActionCreator<TPayload> | A new Action. |
Type Parameters
Name | Description |
---|---|
TPayload | The type of the payload. |
CreateAsyncThunk<TPayload, TThunkArg>(string, PayloadCreator, AsyncThunkOptions<TThunkArg>)
Create a new Async Thunk Action. See AsyncThunkActionCreator<TPayload, TThunkArg> for more information.
Declaration
public static AsyncThunkActionCreator<TPayload, TThunkArg> CreateAsyncThunk<TPayload, TThunkArg>(string type, AsyncThunkActionCreator<TPayload, TThunkArg>.PayloadCreator payloadCreator, AsyncThunkOptions<TThunkArg> options = null)
Parameters
Type | Name | Description |
---|---|---|
string | type | The type of the action. |
AsyncThunkActionCreator<TPayload, TThunkArg>.PayloadCreator | payloadCreator | The payload creator. |
AsyncThunkOptions<TThunkArg> | options | The options for the async thunk. |
Returns
Type | Description |
---|---|
AsyncThunkActionCreator<TPayload, TThunkArg> | A new Async Thunk Action. |
Type Parameters
Name | Description |
---|---|
TPayload | The type of the payload. |
TThunkArg | The type of the argument to pass to the thunk. |
CreateReducer<TState>(TState, Action<ReducerSwitchBuilder<TState>>)
Create reducers for a state slice. See SliceReducerSwitchBuilder<TState> for more information.
Declaration
public static Reducer CreateReducer<TState>(TState initialState, Action<ReducerSwitchBuilder<TState>> builderCallback)
Parameters
Type | Name | Description |
---|---|---|
TState | initialState | The initial state of the state slice. |
Action<ReducerSwitchBuilder<TState>> | builderCallback | The builder that will be used to create the reducers. |
Returns
Type | Description |
---|---|
Reducer | A reducer record that can be used to create a state slice. |
Type Parameters
Name | Description |
---|---|
TState | The type of the state. |
CreateSlice<TState>(string, TState, Action<SliceReducerSwitchBuilder<TState>>, Action<ReducerSwitchBuilder<TState>>)
Create a new state slice. A state slice is a part of the state tree. You can provide reducers that will "own" the state slice at the same time.
Declaration
public Slice<TState> CreateSlice<TState>(string name, TState initialState, Action<SliceReducerSwitchBuilder<TState>> reducers, Action<ReducerSwitchBuilder<TState>> extraReducers = null)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the state slice. |
TState | initialState | The initial state of the state slice. |
Action<SliceReducerSwitchBuilder<TState>> | reducers | The reducers that will "own" the state slice. |
Action<ReducerSwitchBuilder<TState>> | extraReducers | The reducers that will be called if the action type does not match any of the main reducers. |
Returns
Type | Description |
---|---|
Slice<TState> | A slice object that can be used to access the state slice. |
Type Parameters
Name | Description |
---|---|
TState | The type of the state. |
Remarks
You can also provide extra reducers that will be called if the action type does not match any of the main reducers.
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the state slice already exists. |
Dispatch(string)
Dispatches an action. This is the only way to trigger a state change.
Declaration
public void Dispatch(string actionType)
Parameters
Type | Name | Description |
---|---|---|
string | actionType | The type of the action. |
Remarks
This method can't be used to dispatch Async Thunk Actions. You need to instantiate an AsyncThunkActionCreator<TPayload, TThunkArg> and call Invoke(TThunkArg) instead.
Dispatch(Action)
Dispatches an action. This is the only way to trigger a state change.
Declaration
public void Dispatch(Action action)
Parameters
Type | Name | Description |
---|---|---|
Action | 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. |
Dispatch<T>(string, T)
Dispatches an action. This is the only way to trigger a state change.
Declaration
public void Dispatch<T>(string actionType, T payload)
Parameters
Type | Name | Description |
---|---|---|
string | actionType | The type of the action. |
T | payload | The payload of the action. |
Type Parameters
Name | Description |
---|---|
T | The type of the payload. |
Remarks
This method can't be used to dispatch Async Thunk Actions. You need to instantiate an AsyncThunkActionCreator<TPayload, TThunkArg> and call Invoke(TThunkArg) instead.
GetState()
Returns the current state tree of your application. It is equal to the last value returned by the store's reducer.
Declaration
public Dictionary<string, object> GetState()
Returns
Type | Description |
---|---|
Dictionary<string, object> | The current state tree of your application. |
GetState<TState>(string)
Returns the current state tree of your application for a specific slice. It is equal to the last value returned by the store's reducer.
Declaration
public TState GetState<TState>(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the state slice. |
Returns
Type | Description |
---|---|
TState | The current state tree of your application. |
Type Parameters
Name | Description |
---|---|
TState | The type of the state. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the state slice does not exist. |
NotifyStateChanged(string)
Force notify all listeners of a state slice.
Declaration
public void NotifyStateChanged(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the state slice. |
Subscribe<TState>(string, Action<TState>)
Adds a change listener. It will be called any time an action is dispatched, and some part of the state tree may potentially have changed.
Declaration
public Unsubscriber Subscribe<TState>(string name, Action<TState> listener)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the state slice. |
Action<TState> | listener | A callback to be invoked on every dispatch. |
Returns
Type | Description |
---|---|
Unsubscriber | A function to remove this change listener. |
Type Parameters
Name | Description |
---|---|
TState | The type of the state. |
Remarks
This method doesn't check for duplicate listeners, so calling it multiple times with the same listener will result in the listener being called multiple times.