{"id":23894559,"url":"https://github.com/kuasha420/mst-persistent-store","last_synced_at":"2025-04-10T13:34:00.657Z","repository":{"id":46904117,"uuid":"361138190","full_name":"kuasha420/mst-persistent-store","owner":"kuasha420","description":"Mobx State Tree Persistent Store Provider Component and Consumer Hook written in TypeScript for React and React Native ","archived":false,"fork":false,"pushed_at":"2024-10-15T09:06:36.000Z","size":272,"stargazers_count":15,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-03-24T12:13:16.986Z","etag":null,"topics":["hooks","mobx","react","react-hooks","react-native","state-management"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kuasha420.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-24T10:57:58.000Z","updated_at":"2024-10-15T09:06:35.000Z","dependencies_parsed_at":"2024-06-20T23:24:04.307Z","dependency_job_id":"37358e1e-9cf3-4706-8195-9fe2c51d3c9e","html_url":"https://github.com/kuasha420/mst-persistent-store","commit_stats":{"total_commits":26,"total_committers":3,"mean_commits":8.666666666666666,"dds":"0.42307692307692313","last_synced_commit":"a737a282259557171587c4e81da4e58d60719b3a"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuasha420%2Fmst-persistent-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuasha420%2Fmst-persistent-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuasha420%2Fmst-persistent-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuasha420%2Fmst-persistent-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kuasha420","download_url":"https://codeload.github.com/kuasha420/mst-persistent-store/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248225846,"owners_count":21068078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","mobx","react","react-hooks","react-native","state-management"],"created_at":"2025-01-04T14:59:38.109Z","updated_at":"2025-04-10T13:34:00.633Z","avatar_url":"https://github.com/kuasha420.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mobx State Tree Persistent Store \u003c!-- omit in toc --\u003e\n\nA factory to easily create Persistent Mobx State Tree Store Provider and consumer hook.\n\n- [Installation](#installation)\n  - [For React](#for-react)\n  - [For React Native](#for-react-native)\n- [Usage](#usage)\n  - [Create Provider and Hooks](#create-provider-and-hooks)\n  - [Add Provider to The Root Component](#add-provider-to-the-root-component)\n  - [Use the Store from Child Components](#use-the-store-from-child-components)\n- [Custom Storage Backend](#custom-storage-backend)\n- [API](#api)\n  - [createPersistentStore](#createpersistentstore)\n    - [Type Definition](#type-definition)\n    - [Arguments](#arguments)\n    - [PersistentStoreOptions](#persistentstoreoptions)\n- [Notes](#notes)\n  - [`disallowList`](#disallowlist)\n- [License](#license)\n- [Contribution](#contribution)\n\n## Installation\n\n`yarn add mst-persistent-store`\n\n`@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](#usage) and [Custom Storage Backend](#custom-storage-backend) for more info about how to use default or custom storage.\n\n### For React\n\n`yarn add mobx-state-tree localforage`\n\n### For React Native\n\n`yarn add mobx-state-tree @react-native-async-storage/async-storage`\n\n## Usage\n\nUsage is very simple.\n\n### Create Provider and Hooks\n\nBelow is an example on how to create the provider and consumer hook.\n\n```ts\n// store-setup.ts\nimport { types } from 'mobx-state-tree';\nimport createPersistentStore from 'mst-persistent-store';\n// The default storage is async-storage for React Native and localforage for Web\n// This needs to be explicitly imported for the default storage to work correctly\n// for the respective platform. If you are using a custom storage,\n// you don't need to import this\nimport defaultStorage from 'mst-persistent-store/dist/storage';\n\nconst PersistentStore = types\n  .model('RootStore', {\n    name: types.string,\n    age: types.number,\n    premium: types.boolean,\n  })\n  .actions((self) =\u003e ({\n    grantPremium() {\n      self.premium = true;\n    },\n  }))\n  .views((self) =\u003e ({\n    get isAdult() {\n      return self.age \u003e= 18;\n    },\n  }));\n\nexport const [PersistentStoreProvider, usePersistentStore] = createPersistentStore(\n  PersistentStore,\n  defaultStorage,\n  {\n    name: 'Test User',\n    age: 19,\n    premium: false,\n  }\n);\n```\n\n### Add Provider to The Root Component\n\nWrap your app with the created Provider component.\n\n```tsx\n// app.tsx\nimport { PersistentStoreProvider } from './store-setup';\nimport Main from './main';\n\nexport default function App() {\n  return (\n    \u003cPersistentStoreProvider\u003e\n      \u003cMain /\u003e\n    \u003c/PersistentStoreProvider\u003e\n  );\n}\n```\n\n### Use the Store from Child Components\n\nConsume store values using the hook.\n\n```tsx\n// main.tsx\nimport { observer } from 'mobx-react-lite';\nimport { useEffect } from 'react';\nimport { usePersistentStore } from './store-setup';\n\nconst Main = observer(() =\u003e {\n  const store = usePersistentStore();\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e\n        {store.name} is {store.age} years old and {store.isAdult ? 'is' : 'is not'} an adult.\n      \u003c/p\u003e\n    \u003c/div\u003e\n  );\n});\n\nexport default Main;\n```\n\n## Custom Storage Backend\n\nThe above example uses the default storage. You can use any storage you want by passing the storage object to the factory.\n\nHere is an example using `react-native-mmkv` as the storage.\n\n```ts\nimport { MMKV } from 'react-native-mmkv';\n\nconst mmkv = new MMKV();\n\nconst setItem = (key: string, value: any) =\u003e mmkv.set(key, JSON.stringify(value));\n\nconst getItem = (key: string) =\u003e {\n  const value = mmkv.getString(key);\n  if (value) {\n    return JSON.parse(value);\n  }\n  return null;\n};\n\nconst removeItem = (key: string) =\u003e mmkv.delete(key);\n\nconst storage = {\n  setItem,\n  getItem,\n  removeItem,\n};\n\nexport const [PersistentStoreProvider, usePersistentStore] = createPersistentStore(\n  PersistentStore,\n  storage,\n  {\n    name: 'Test User',\n    age: 19,\n    premium: false,\n  }\n);\n```\n\n## API\n\n### createPersistentStore\n\n#### Type Definition\n\n```ts\nexport interface StorageOptions {\n  setItem: (key: string, value: any) =\u003e Promise\u003cvoid\u003e | void;\n  getItem: (key: string) =\u003e Promise\u003cany | null\u003e | any | null;\n  removeItem: (key: string) =\u003e Promise\u003cvoid\u003e | void;\n}\n\ninterface PersistentStoreOptions\u003cT extends IAnyModelType\u003e {\n  storageKey: string;\n  writeDelay: number;\n  logging: boolean;\n  devtool: boolean;\n  onHydrate?: (store: Instance\u003cT\u003e) =\u003e void;\n}\nconst createPersistentStore: \u003cT extends IAnyModelType\u003e(\n  store: T,\n  storage: StorageOptions,\n  init: SnapshotIn\u003cT\u003e,\n  disallowList?: PartialDeep\u003cSnapshotIn\u003cT\u003e\u003e,\n  options?: Partial\u003cPersistentStoreOptions\u003cT\u003e\u003e\n) =\u003e readonly [React.FC, () =\u003e Instance\u003cT\u003e];\n```\n\n#### Arguments\n\n| param        | type                              | required | description                                                                                                                                                                                                     |\n| ------------ | --------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| store        | `T extends IAnyModelType`         | yes      | the mst model to instantiate                                                                                                                                                                                    |\n| 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. |\n| init         | `SnapshotIn\u003cT\u003e`                   | yes      | the init data of the store                                                                                                                                                                                      |\n| disallowList | `PartialDeep\u003cSnapshotIn\u003cT\u003e\u003e`      | no       | the part of the store that should not be persisted. See notes below                                                                                                                                             |\n| options      | `Partial\u003cPersistentStoreOptions\u003e` | no       | Various options to change store behavior                                                                                                                                                                        |\n\n#### PersistentStoreOptions\n\nAll Properties are optional.\n\n| property   | type                           | default                      | description                                                                                                                                                                 |\n| ---------- | ------------------------------ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| storageKey | `string`                       | persistentStore              | the key to use as the localforage key. Must be \u003cbr\u003echanged when using multiple stores in the same\u003cbr\u003eapp to avoid overriding data.                                          |\n| writeDelay | `number`                       | 1500                         | On Repeated Store Update, it's advisable to wait\u003cbr\u003ea certain time before updating the persistent \u003cbr\u003estorage with new snapshot. This value controls the\u003cbr\u003edebounce delay. |\n| logging    | `boolean`                      | true is dev\u003cbr\u003efalse in prod | Whether to enable logging.                                                                                                                                                  |\n| devtool    | `boolean`                      | true in dev\u003cbr\u003efalse in prod | Whether to integrate with mobx-devtool                                                                                                                                      |\n| onHydrate  | `(store: Instance\u003cT\u003e) =\u003e void` | none                         | Callback to run after hydration is done.                                                                                                                                    |\n\n## Notes\n\n### `disallowList`\n\n`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.\n\nThis is a deep partial of the store snapshot. Anything passed here will replace the value on hydration.\n\n## License\n\nThis package is licensed under the MIT License.\n\n## Contribution\n\nAny kind of contribution is welcome. Thanks!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuasha420%2Fmst-persistent-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkuasha420%2Fmst-persistent-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuasha420%2Fmst-persistent-store/lists"}