# expo-use-memory-value

> Expo integration for stored memory values

Latest version **8.0.0** (published 2025-01-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install expo-use-memory-value
pnpm add expo-use-memory-value
yarn add expo-use-memory-value
bun add expo-use-memory-value
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2025-01-06 |
| First published | 2020-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Derk-Jan Karrenbeld |
| Maintainers | sleeplessbyte |

## Links

- npm: https://www.npmjs.com/package/expo-use-memory-value
- Repository: https://github.com/SleeplessByte/expo-use-memory-value
- Homepage: https://github.com/SleeplessByte/expo-use-memory-value#readme
- Issues: https://github.com/SleeplessByte/expo-use-memory-value/issues
- npm.io page: https://npm.io/package/expo-use-memory-value

## Dependencies (2)

- [mitt](https://npm.io/package/mitt.md) ^3.0.1
- [react-fast-compare](https://npm.io/package/react-fast-compare.md) ^3.2.2

## Recent versions

- 8.0.0 (latest) — 2025-01-06
- 6.0.4 — 2024-02-13
- 6.0.3 — 2024-02-10
- 6.0.2 — 2024-02-10
- 6.0.1 — 2024-02-10
- 6.0.0 — 2024-02-10
- 5.0.0 — 2021-12-27
- 4.1.0 — 2021-05-08
- 4.0.0 — 2021-02-25
- 3.0.3 — 2020-07-21
- 3.0.2 — 2020-07-20
- 3.0.1 — 2020-07-19
- 3.0.0 — 2020-07-19
- 2.1.2 — 2020-06-25
- 2.1.1 — 2020-06-25
- … 3 more at https://npm.io/package/expo-use-memory-value/versions

## README

# expo-use-memory-value

Hooks for global state in memory and local storage (platform dependent).
It's like context, but only one state per value; allows you to subscribe and update values from far away.

Tested against Expo SDK 50.

If you're looking for non expo implementation: [`use-memory-value`](https://github.com/SleeplessByte/use-memory-value).

## Installation

```bash
yarn add expo-use-memory-value
```

It has the following peerDependencies, along with `react` and `react-native` / `react-native-web`:

```bash
yarn expo add @react-native-async-storage/async-storage expo-secure-store localforage
# assumes already installed:
# - react
# - react-native
# - react-native-web
```

If you _only_ target the web, you can use the following, and ignore the warnings about `async-storage` and `expo-secure-store`:

```bash
yarn add localforage
# assumes already installed:
# - react
# - react-native-web
```

If you _do not_ target the web, you can use the following, and ignore the warnings about `localforage`:

```bash
yarn expo add @react-native-async-storage/async-storage expo-secure-store
# assumes already installed:
# - react
# - react-native
```

## Usage

Start by creating a new `MemoryValue` or `StoredMemoryValue`. You can declare this in any file, make sure it's exported and importable from all the files you want to use the value.

```typescript
import { MemoryValue } from 'use-memory-value';

type State = {
  foo: number;
  bar: string;
  baz?: boolean;
};

const INITIAL_STATE: State = {
  foo: 42,
  bar: 'yes',
};

export const MY_STATE = new MemoryValue<State>(INITIAL_STATE);
```

Then, where you want to use the value, import the `MemoryValue` and `useGlobalValue`:

```tsx
import { useGlobalValue } from 'use-memory-value';
import { MY_STATE } from '../path/to/state';

function ReadOnlyBar() {
  const [state] = useGlobalValue(MY_STATE);
  return <h1>foo: {state && state.foo}</h1>;
}

function CountingFoo() {
  const [state, updateState] = useGlobalValue(MY_STATE);
  const increment = () =>
    updateState((prev) => ({ ...prev, foo: prev.foo + 1 }));

  return (
    <button type="button" onClick={increment}>
      Foo: {state.foo}
    </button>
  );
}

function ActivateBaz() {
  const [, updateState] = useGlobalValue(MY_STATE);
  const activate = () => updateState((prev) => ({ ...prev, baz: true }));

  return (
    <button type="button" onClick={activate}>
      Activate
    </button>
  );
}
```

If the value should be persisted to (and initialized from) local storage, use `StoredMemoryValue`:

```typescript
export const MY_STATE = new StoredMemoryValue<State>('local.key.name');
```

## TypeScript warnings

It is _important_ to use `type` and not `interface` when using this in conjunction with TypeScript.
The reason for this is that `interfaces` are _extendible_ and thus we can not safely say that the final resolved shape is serializable (JSON-compatible). `types` are fixed, and thus can be checked.

You want this because non-serializable fields would be lost during serialization/deserialization, and thus can cause run-time issues.

If you get errors that the type is not `Serializable`, make sure you're only using `type` and not `interface`.

## Android/iOS secure storage

Swap `StoredMemoryValue` for `SecureStoreMemoryValue`. This will use `expo-secure-storage` under the hood for native platforms. On the `web` it falls back to `localforage`. **Values on the web are never secure**.

Note: There are limitations to storage size.

## Web configuration

For the web it uses `localforage` under the hood. You can set the instance yourself by importing

```typescript
import { setLocalForageInstance } from 'expo-use-memory-value/storage.web';
```

---
_Source: https://npm.io/package/expo-use-memory-value · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
