Store manager

2 minutes read

Applies to 1.1.x
State DRAFT

The store manager class implements all the methods and functions that simplifies the interaction and advanced management of Redux state, reducers and actions.

Initialization

It implements the singleton pattern through the "getInstance" function and accepts a configuration object that is used to initialize and reconfigure the Redux state slices and reducers. The configuration object must implement the "IReduxConfig" interface.

// initialize store and configure Redux
const storeManager = StoreManager.getInstance(initialReduxConfig);

// get the store manager instance after initialization
const storeManager = StoreManager.getInstance();

The initialization of an empty store is supported to enable dynamic configuration at runtime. Because an empty store is not allowed by Redux, the "hypereact" state library Internally uses a fallback slice (with key "_") and an empty reducer that just does not implement any logic and that is removed when the first slice and reducer is added.

Store (Redux)

The Redux store that is managed internally is accessible through the "getStore" function to be used within React providers and React Redux.

// get store for Redux-ready React provider
const reduxStore = StoreManager.getInstance().getStore();

ReactDOM.render(
  <Provider store={reduxStore}>
    <App />
  </Provider>,
  document.getElementById("root")
);

Access State

The store manager provides convenient way to access root state and state slices programmatically through the "getState" function.

const storeManager = StoreManager.getInstance();

// get the Redux root state
const rootState = storeManager.getState();

// get a slice state (example from the getting started code)
const state: ExampleState = storeManager.getState("example");

Dispatch Actions

The store manager implements a "dispatch" method that enables support for "hypereact" enhanced features (e.g. reduce-able actions, built-in reducers, ...). The action dispatching should be always executed through the the store manager.

const storeManager = StoreManager.getInstance();

// dispatch a generic Redux action
const jsonAction = {
  type: "UNMANAGED_ACTION"
};
storeManager.dispatch(jsonAction);

// dispatch a class typed action (example from the getting started code)
const action = new SetExampleMessageAction("...");
storeManager.dispatch(action);