Updated
Rematch plugin for maintaining timestamps when an effect is triggered.
Updated is primarily used for optimizing effects. It can be used to:
- prevent expensive fetch requests within a certain time period
- throttle effects
Compatibilityβ
Install the correct version of the updated plugin based on the version of the core Rematch library in your project.
| @rematch/core | @rematch/updated |
|---|---|
| 1.x.x | 1.x.x |
| 2.x.x | 2.x.x |
Installβ
- npm
- Yarn
bashnpm install @rematch/updated
bashnpm install @rematch/updated
bashyarn add @rematch/updated
bashyarn add @rematch/updated
updatedPlugin([config])β
The updated plugin accepts one optional argument - config, which is an object with the following properties:
name(string): the key for the updated state. Default value isupdated.blacklist(string[]): list of blacklisted model names, for which the plugin will not track effectsdateCreator(() => any): by default it's a function which returns new Date object when an effect is called. However, if you prefer to use moment or any other custom library, you can provide a custom implementation, such as() => moment().
Usageβ
Letβs say we have a model βcountβ in our store which has two effects - fetchOne and fetchTwo. Updated pluginβs state will have the following format:
js{"count": {"fetchOne": "2020-12-13T20:48:34.935Z", // Date when fetchOne effect was last fetched"fetchTwo": "2020-12-13T20:40:34.935Z" , // Date when fetchTwo effect was last fetched}}
js{"count": {"fetchOne": "2020-12-13T20:48:34.935Z", // Date when fetchOne effect was last fetched"fetchTwo": "2020-12-13T20:40:34.935Z" , // Date when fetchTwo effect was last fetched}}
Setup the storeβ
To use the plugin, start with adding it to your store:
- JavaScript
- TypeScript
store.jsjsimport updatedPlugin from "@rematch/updated";import { init } from "@rematch/core";import * as models from "./models";init({models,plugins: [updatedPlugin()],});
store.jsjsimport updatedPlugin from "@rematch/updated";import { init } from "@rematch/core";import * as models from "./models";init({models,plugins: [updatedPlugin()],});
store.tstsimport updatedPlugin, { ExtraModelsFromUpdated } from "@rematch/updated";import { init, RematchDispatch, RematchRootState } from "@rematch/core";import { models, RootModel } from "./models";type FullModel = ExtraModelsFromUpdated<RootModel>;// Also you can use loading plugin// type FullModel = ExtraModelsFromLoading<RootModel> & ExtraModelsFromUpdated<RootModel>export const store = init<RootModel, FullModel>({models,plugins: [updatedPlugin()],});export type Store = typeof store;export type Dispatch = RematchDispatch<RootModel>;export type RootState = RematchRootState<RootModel, FullModel>;
store.tstsimport updatedPlugin, { ExtraModelsFromUpdated } from "@rematch/updated";import { init, RematchDispatch, RematchRootState } from "@rematch/core";import { models, RootModel } from "./models";type FullModel = ExtraModelsFromUpdated<RootModel>;// Also you can use loading plugin// type FullModel = ExtraModelsFromLoading<RootModel> & ExtraModelsFromUpdated<RootModel>export const store = init<RootModel, FullModel>({models,plugins: [updatedPlugin()],});export type Store = typeof store;export type Dispatch = RematchDispatch<RootModel>;export type RootState = RematchRootState<RootModel, FullModel>;
Use in the viewβ
Define a model which uses effects.
some-model.jsjsexport const count = {...,effects: {async fetchOne() {},async fetchTwo() {},}}
some-model.jsjsexport const count = {...,effects: {async fetchOne() {},async fetchTwo() {},}}
Use the updated state:
someView.jsxjsconst state = store.getState();// or just connect() on `react-redux`console.log(state.updated.count.fetchOne);console.log(state.updated.count.fetchTwo);
someView.jsxjsconst state = store.getState();// or just connect() on `react-redux`console.log(state.updated.count.fetchOne);console.log(state.updated.count.fetchTwo);