Store
Store returned by Rematch is essentially a Redux store with a few additional properties and extra features. Therefore, for more in-depth reference of the functions 'provided by Redux', refer to the Redux documentation.
Configuration​
Store is an object which contains the following properties:
name
(string): name of a storeaddModel(namedModel)
: it allows to lazy-load models and merge them into Rematch after init has been called. addModel accepts namedModel which means it needs to have parametername
defined (see example below).getState()
: provided by Redux, returns the state of your store.subscribe(listener: () => void): Unsubscribe
: provided by Redux, adds a change listener.replaceReducer(nextReducer): void
: provided by Redux, replaces the reducer currently used by the store to calculate the state.dispatch(action)
: provided by Redux, a function that dispatches actions to the store. However, in Rematch dispatch can be also used as an object which contains action dispatchers (see example below).
Dispatch actions directly from store​
js
import store from "./store";// dispatch reducers actionsstore.dispatch({ type: "count/increment", payload: 1 }); // regular dispatch usagestore.dispatch.count.increment(1); // the same as above but with action dispatcher// dispatch effects actionsstore.dispatch({ type: "count/incrementAsync", payload: 1 }); // regular dispatch usagestore.dispatch.count.incrementAsync(1); // the same as above but with action dispatcher
js
import store from "./store";// dispatch reducers actionsstore.dispatch({ type: "count/increment", payload: 1 }); // regular dispatch usagestore.dispatch.count.increment(1); // the same as above but with action dispatcher// dispatch effects actionsstore.dispatch({ type: "count/incrementAsync", payload: 1 }); // regular dispatch usagestore.dispatch.count.incrementAsync(1); // the same as above but with action dispatcher
Add dynamic models​
js
import store from "./store";// (1) initiallystore.getState(); // { count: 0 }// (2) add modelstore.addModel({ name: "countB", state: 99 });// (3) after addingstore.getState(); // { count: 0, countB: 99 }
js
import store from "./store";// (1) initiallystore.getState(); // { count: 0 }// (2) add modelstore.addModel({ name: "countB", state: 99 });// (3) after addingstore.getState(); // { count: 0, countB: 99 }