createStore

information

By default, the export package name for all APIs in this section is: @modern-js/runtime/model.

If Reduck is integrated separately from Modern.js, the export package name is: @modern-js-reduck/react.

createStore is used to create a Store. Reduck’s Store based on Redux's Store implementatio. Used to store the state of the application and managing the state and Model.

In general, this API is only used when you need to have full control over the creation of the Store. For example, to customize a Store, pass in the 'Provider' component to use.

Function Signature

interface StoreConfig {
  initialState?: Record<string, any>;
  middlewares?: Middleware[];
  models?: Model[];
  plugins?: Plugin[];
  enhancers?: StoreEnhancer[];
}

interface ReduckStore extends ReduxStore {
  use: typeof useModel;
  unmount: (model: Model) => void;
}

function createStore(config?: StoreConfig): ReduckStore;

Input

  • config: store options.
    • initialState: set the initial state for store.
    • models: set the Model to mount to the Store in advance(No need for normal use).
    • middlewares: set Redux middleware.
    • enhancers: set Redux Store enhancer.
    • plugins: set Reduck plugin.experimental API, not recommended.

Return Value

Reduck Store:

  • use: mount and fetch Model objects dynamically. Usage is the same as 'useModel', but can be used outside of React components.
  • unmount: unmount the Model object, and the Model State is cleared from the Store.
  • ReduxStore: Redux Store API.

Example

const store = createStore();

function load() {
  const [, actions] = store.use(fooModel);

  actions.load();
}