Gathering detailed insights and metrics for mst-persistent-store
Gathering detailed insights and metrics for mst-persistent-store
Gathering detailed insights and metrics for mst-persistent-store
Gathering detailed insights and metrics for mst-persistent-store
Mobx State Tree Persistent Store Provider Component and Consumer Hook written in TypeScript for React and React Native
npm install mst-persistent-store
Typescript
Module System
Node Version
NPM Version
TypeScript (99.4%)
JavaScript (0.6%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
16 Stars
69 Commits
3 Forks
1 Watchers
2 Branches
3 Contributors
Updated on Apr 09, 2025
Latest Version
3.1.1
Package Id
mst-persistent-store@3.1.1
Unpacked Size
56.34 kB
Size
15.42 kB
File Count
55
NPM Version
8.12.1
Node Version
18.4.0
Published on
Oct 09, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
4
20
A factory to easily create Persistent Mobx State Tree Store Provider and consumer hook.
yarn add mst-persistent-store
@react-native-async-storage/async-storage
and localforage
are optional peer dependencies. You can use any storage you want by passing the storage object to the factory. But if you want to use the default storage, you need to install one of them. See Usage and Custom Storage Backend for more info about how to use default or custom storage.
yarn add mobx-state-tree localforage
yarn add mobx-state-tree @react-native-async-storage/async-storage
Usage is very simple.
Below is an example on how to create the provider and consumer hook.
1// store-setup.ts 2import { types } from 'mobx-state-tree'; 3import createPersistentStore from 'mst-persistent-store'; 4import defaultStorage from 'mst-persistent-store/dist/storage'; 5 6const PersistentStore = types 7 .model('RootStore', { 8 name: types.string, 9 age: types.number, 10 premium: types.boolean, 11 hydrated: types.boolean, 12 }) 13 .actions((self) => ({ 14 hydrate() { 15 self.hydrated = true; 16 }, 17 })) 18 .views((self) => ({ 19 get isAdult() { 20 return self.age >= 18; 21 }, 22 })); 23 24export const [PersistentStoreProvider, usePersistentStore] = createPersistentStore( 25 PersistentStore, 26 defaultStorage, 27 { 28 name: 'Test User', 29 age: 19, 30 premium: false, 31 hydrated: false, 32 }, 33 { 34 hydrated: false, 35 }, 36 { 37 logging: false, 38 devtool: false, 39 } 40);
Wrap your app with the created Provider component.
1// app.tsx 2import { PersistentStoreProvider } from './store-setup'; 3import Main from './main'; 4 5export default function App() { 6 return ( 7 <PersistentStoreProvider> 8 <Main /> 9 </PersistentStoreProvider> 10 ); 11}
Consume store values using the hook.
1// main.tsx 2import { observer } from 'mobx-react-lite'; 3import { useEffect } from 'react'; 4import { usePersistentStore } from './store-setup'; 5 6const Main = observer(() => { 7 const { name, age, isAdult, hydrated, hydrate } = usePersistentStore(); 8 9 useEffect(() => { 10 hydrate(); 11 }, []); 12 13 if (!hydrated) { 14 return <p>Loading...</p>; 15 } 16 17 return ( 18 <div> 19 <p> 20 {name} is {age} years old and {isAdult ? 'is' : 'is not'} an adult. 21 </p> 22 </div> 23 ); 24}); 25 26export default Main;
The above example uses the default storage. You can use any storage you want by passing the storage object to the factory.
Here is an example using react-native-mmkv
as the storage.
1import { MMKV } from 'react-native-mmkv'; 2 3const mmkv = new MMKV(); 4 5const setItem = (key: string, value: any) => mmkv.set(key, JSON.stringify(value)); 6 7const getItem = (key: string) => { 8 const value = mmkv.getString(key); 9 if (value) { 10 return JSON.parse(value); 11 } 12 return null; 13}; 14 15const removeItem = (key: string) => mmkv.delete(key); 16 17const storage = { 18 setItem, 19 getItem, 20 removeItem, 21}; 22 23export const [PersistentStoreProvider, usePersistentStore] = createPersistentStore( 24 PersistentStore, 25 storage, 26 { 27 name: 'Test User', 28 age: 19, 29 premium: false, 30 hydrated: false, 31 }, 32 { 33 hydrated: false, 34 }, 35 { 36 hydrated: false, 37 } 38);
1export interface StorageOptions { 2 setItem: (key: string, value: any) => Promise<void> | void; 3 getItem: (key: string) => Promise<any | null> | any | null; 4 removeItem: (key: string) => Promise<void> | void; 5} 6 7interface PersistentStoreOptions<T extends IAnyModelType> { 8 storageKey: string; 9 writeDelay: number; 10 logging: boolean; 11 devtool: boolean; 12 onHydrate?: (store: Instance<T>) => void; 13} 14const createPersistentStore: <T extends IAnyModelType>( 15 store: T, 16 storage: StorageOptions, 17 init: SnapshotIn<T>, 18 disallowList?: PartialDeep<SnapshotIn<T>>, 19 options?: Partial<PersistentStoreOptions<T>> 20) => readonly [React.FC, () => Instance<T>];
param | type | required | description |
---|---|---|---|
store | T extends IAnyModelType | yes | the mst model to instantiate |
storage | StorageOptions | yes | the storage to use. Use defaultStorage from mst-persistent-store/dist//storage to use the @react-native-async-storage/async-storage (for React Native) or localforage (for Web) backed default storage. |
init | SnapshotIn<T> | yes | the init data of the store |
disallowList | PartialDeep<SnapshotIn<T>> | no | the part of the store that should not be persisted. See notes below |
options | Partial<PersistentStoreOptions> | no | Various options to change store behavior |
All Properties are optional.
property | type | default | description |
---|---|---|---|
storageKey | string | persistentStore | the key to use as the localforage key. Must be changed when using multiple stores in the same app to avoid overriding data. |
writeDelay | number | 1500 | On Repeated Store Update, it's advisable to wait a certain time before updating the persistent storage with new snapshot. This value controls the debounce delay. |
logging | boolean | true is dev false in prod | Whether to enable logging. |
devtool | boolean | true in dev false in prod | Whether to integrate with mobx-devtool |
onHydrate | (store: Instance<T>) => void | none | Callback to run after hydration is done. |
disallowList
disallowList
is used to specify the part of the store that should not be persisted. This is useful when you have some part of the store that should not be persisted. For example, you may have a part of the store that is used for UI state management and should not be persisted.
This is a deep partial of the store snapshot. Anything passed here will replace the value on hydration.
This package is licensed under the MIT License.
Any kind of contribution is welcome. Thanks!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 0/7 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More