# mst-persistent-store

> Mobx State Tree Persistant Store Provider Component and Consumer Hook writen in TypeScript

Latest version **3.1.1** (published 2024-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install mst-persistent-store
pnpm add mst-persistent-store
yarn add mst-persistent-store
bun add mst-persistent-store
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2024-10-09 |
| First published | 2021-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 56.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Arafat Zahan |
| Maintainers | kuasha420 |
| Keywords | mst-persist, mobx-state-tree-hook, mst-provider, mst-hook, mst-react-native, mobx-state-tree |

## Links

- npm: https://www.npmjs.com/package/mst-persistent-store
- Repository: https://github.com/kuasha420/mst-persistent-store
- Homepage: https://github.com/kuasha420/mst-persistent-store#readme
- Issues: https://github.com/kuasha420/mst-persistent-store/issues
- npm.io page: https://npm.io/package/mst-persistent-store

## Dependencies (3)

- [use-async-effect](https://npm.io/package/use-async-effect.md) 2.2.7
- [mobx-devtools-mst](https://npm.io/package/mobx-devtools-mst.md) 0.9.30
- [@hyperjump/json-pointer](https://npm.io/package/@hyperjump/json-pointer.md) 1.1.0

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 3.1.1 (latest) — 2024-10-09
- 3.1.0 — 2024-10-08
- 3.0.2 — 2024-09-15
- 3.0.1 — 2023-12-18
- 3.0.0 — 2023-12-18
- 2.1.1 — 2023-12-18
- 2.1.0 — 2023-12-18
- 2.0.0 — 2023-12-05
- 1.2.6 — 2023-07-11
- 1.2.5 — 2022-07-24
- 1.2.4 — 2022-07-24
- 1.2.3 — 2022-04-23
- 1.2.2 — 2021-07-06
- 1.2.1 — 2021-05-10
- 1.2.0 — 2021-05-06
- … 5 more at https://npm.io/package/mst-persistent-store/versions

## README

# Mobx State Tree Persistent Store <!-- omit in toc -->

A factory to easily create Persistent Mobx State Tree Store Provider and consumer hook.

- [Installation](#installation)
  - [For React](#for-react)
  - [For React Native](#for-react-native)
- [Usage](#usage)
  - [Create Provider and Hooks](#create-provider-and-hooks)
  - [Add Provider to The Root Component](#add-provider-to-the-root-component)
  - [Use the Store from Child Components](#use-the-store-from-child-components)
- [Custom Storage Backend](#custom-storage-backend)
- [API](#api)
  - [createPersistentStore](#createpersistentstore)
    - [Type Definition](#type-definition)
    - [Arguments](#arguments)
    - [PersistentStoreOptions](#persistentstoreoptions)
- [Notes](#notes)
  - [`disallowList`](#disallowlist)
- [License](#license)
- [Contribution](#contribution)

## Installation

`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](#usage) and [Custom Storage Backend](#custom-storage-backend) for more info about how to use default or custom storage.

### For React

`yarn add mobx-state-tree localforage`

### For React Native

`yarn add mobx-state-tree @react-native-async-storage/async-storage`

## Usage

Usage is very simple.

### Create Provider and Hooks

Below is an example on how to create the provider and consumer hook.

```ts
// store-setup.ts
import { types } from 'mobx-state-tree';
import createPersistentStore from 'mst-persistent-store';
import defaultStorage from 'mst-persistent-store/dist/storage';

const PersistentStore = types
  .model('RootStore', {
    name: types.string,
    age: types.number,
    premium: types.boolean,
    hydrated: types.boolean,
  })
  .actions((self) => ({
    hydrate() {
      self.hydrated = true;
    },
  }))
  .views((self) => ({
    get isAdult() {
      return self.age >= 18;
    },
  }));

export const [PersistentStoreProvider, usePersistentStore] = createPersistentStore(
  PersistentStore,
  defaultStorage,
  {
    name: 'Test User',
    age: 19,
    premium: false,
    hydrated: false,
  },
  {
    hydrated: false,
  },
  {
    logging: false,
    devtool: false,
  }
);
```

### Add Provider to The Root Component

Wrap your app with the created Provider component.

```tsx
// app.tsx
import { PersistentStoreProvider } from './store-setup';
import Main from './main';

export default function App() {
  return (
    <PersistentStoreProvider>
      <Main />
    </PersistentStoreProvider>
  );
}
```

### Use the Store from Child Components

Consume store values using the hook.

```tsx
// main.tsx
import { observer } from 'mobx-react-lite';
import { useEffect } from 'react';
import { usePersistentStore } from './store-setup';

const Main = observer(() => {
  const { name, age, isAdult, hydrated, hydrate } = usePersistentStore();

  useEffect(() => {
    hydrate();
  }, []);

  if (!hydrated) {
    return <p>Loading...</p>;
  }

  return (
    <div>
      <p>
        {name} is {age} years old and {isAdult ? 'is' : 'is not'} an adult.
      </p>
    </div>
  );
});

export default Main;
```

## Custom Storage Backend

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.

```ts
import { MMKV } from 'react-native-mmkv';

const mmkv = new MMKV();

const setItem = (key: string, value: any) => mmkv.set(key, JSON.stringify(value));

const getItem = (key: string) => {
  const value = mmkv.getString(key);
  if (value) {
    return JSON.parse(value);
  }
  return null;
};

const removeItem = (key: string) => mmkv.delete(key);

const storage = {
  setItem,
  getItem,
  removeItem,
};

export const [PersistentStoreProvider, usePersistentStore] = createPersistentStore(
  PersistentStore,
  storage,
  {
    name: 'Test User',
    age: 19,
    premium: false,
    hydrated: false,
  },
  {
    hydrated: false,
  },
  {
    hydrated: false,
  }
);
```

## API

### createPersistentStore

#### Type Definition

```ts
export interface StorageOptions {
  setItem: (key: string, value: any) => Promise<void> | void;
  getItem: (key: string) => Promise<any | null> | any | null;
  removeItem: (key: string) => Promise<void> | void;
}

interface PersistentStoreOptions<T extends IAnyModelType> {
  storageKey: string;
  writeDelay: number;
  logging: boolean;
  devtool: boolean;
  onHydrate?: (store: Instance<T>) => void;
}
const createPersistentStore: <T extends IAnyModelType>(
  store: T,
  storage: StorageOptions,
  init: SnapshotIn<T>,
  disallowList?: PartialDeep<SnapshotIn<T>>,
  options?: Partial<PersistentStoreOptions<T>>
) => readonly [React.FC, () => Instance<T>];
```

#### Arguments

| 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                                                                                                                                                                        |

#### PersistentStoreOptions

All Properties are optional.

| property   | type                           | default                      | description                                                                                                                                                                 |
| ---------- | ------------------------------ | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| storageKey | `string`                       | persistentStore              | the key to use as the localforage key. Must be <br>changed when using multiple stores in the same<br>app to avoid overriding data.                                          |
| writeDelay | `number`                       | 1500                         | On Repeated Store Update, it's advisable to wait<br>a certain time before updating the persistent <br>storage with new snapshot. This value controls the<br>debounce delay. |
| logging    | `boolean`                      | true is dev<br>false in prod | Whether to enable logging.                                                                                                                                                  |
| devtool    | `boolean`                      | true in dev<br>false in prod | Whether to integrate with mobx-devtool                                                                                                                                      |
| onHydrate  | `(store: Instance<T>) => void` | none                         | Callback to run after hydration is done.                                                                                                                                    |

## Notes

### `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.

## License

This package is licensed under the MIT License.

## Contribution

Any kind of contribution is welcome. Thanks!

---
_Source: https://npm.io/package/mst-persistent-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
