{"id":17472457,"url":"https://github.com/sleeplessbyte/expo-use-memory-value","last_synced_at":"2026-01-18T08:31:43.123Z","repository":{"id":38882953,"uuid":"268187029","full_name":"SleeplessByte/expo-use-memory-value","owner":"SleeplessByte","description":"Hooks to store a value in memory, and optionally in async or secure storage.","archived":false,"fork":false,"pushed_at":"2024-06-18T00:26:47.000Z","size":1126,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-18T19:46:15.583Z","etag":null,"topics":[],"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/SleeplessByte.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":"2020-05-31T01:10:44.000Z","updated_at":"2021-12-27T22:56:23.000Z","dependencies_parsed_at":"2024-02-22T10:37:40.310Z","dependency_job_id":"c1a5203f-0d50-4ad5-ac34-e9997ac2a9b3","html_url":"https://github.com/SleeplessByte/expo-use-memory-value","commit_stats":{"total_commits":51,"total_committers":2,"mean_commits":25.5,"dds":0.4509803921568627,"last_synced_commit":"2f50af23d38918e6dd86e0b2e014df605916f402"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleeplessByte%2Fexpo-use-memory-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleeplessByte%2Fexpo-use-memory-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleeplessByte%2Fexpo-use-memory-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleeplessByte%2Fexpo-use-memory-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SleeplessByte","download_url":"https://codeload.github.com/SleeplessByte/expo-use-memory-value/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247112739,"owners_count":20885605,"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":[],"created_at":"2024-10-18T17:17:54.785Z","updated_at":"2026-01-18T08:31:43.091Z","avatar_url":"https://github.com/SleeplessByte.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# expo-use-memory-value\n\nHooks for global state in memory and local storage (platform dependent).\nIt's like context, but only one state per value; allows you to subscribe and update values from far away.\n\nTested against Expo SDK 50.\n\nIf you're looking for non expo implementation: [`use-memory-value`](https://github.com/SleeplessByte/use-memory-value).\n\n## Installation\n\n```bash\nyarn add expo-use-memory-value\n```\n\nIt has the following peerDependencies, along with `react` and `react-native` / `react-native-web`:\n\n```bash\nyarn expo add @react-native-async-storage/async-storage expo-secure-store localforage\n# assumes already installed:\n# - react\n# - react-native\n# - react-native-web\n```\n\nIf you _only_ target the web, you can use the following, and ignore the warnings about `async-storage` and `expo-secure-store`:\n\n```bash\nyarn add localforage\n# assumes already installed:\n# - react\n# - react-native-web\n```\n\nIf you _do not_ target the web, you can use the following, and ignore the warnings about `localforage`:\n\n```bash\nyarn expo add @react-native-async-storage/async-storage expo-secure-store\n# assumes already installed:\n# - react\n# - react-native\n```\n\n## Usage\n\nStart 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.\n\n```typescript\nimport { MemoryValue } from 'use-memory-value';\n\ntype State = {\n  foo: number;\n  bar: string;\n  baz?: boolean;\n};\n\nconst INITIAL_STATE: State = {\n  foo: 42,\n  bar: 'yes',\n};\n\nexport const MY_STATE = new MemoryValue\u003cState\u003e(INITIAL_STATE);\n```\n\nThen, where you want to use the value, import the `MemoryValue` and `useGlobalValue`:\n\n```tsx\nimport { useGlobalValue } from 'use-memory-value';\nimport { MY_STATE } from '../path/to/state';\n\nfunction ReadOnlyBar() {\n  const [state] = useGlobalValue(MY_STATE);\n  return \u003ch1\u003efoo: {state \u0026\u0026 state.foo}\u003c/h1\u003e;\n}\n\nfunction CountingFoo() {\n  const [state, updateState] = useGlobalValue(MY_STATE);\n  const increment = () =\u003e\n    updateState((prev) =\u003e ({ ...prev, foo: prev.foo + 1 }));\n\n  return (\n    \u003cbutton type=\"button\" onClick={increment}\u003e\n      Foo: {state.foo}\n    \u003c/button\u003e\n  );\n}\n\nfunction ActivateBaz() {\n  const [, updateState] = useGlobalValue(MY_STATE);\n  const activate = () =\u003e updateState((prev) =\u003e ({ ...prev, baz: true }));\n\n  return (\n    \u003cbutton type=\"button\" onClick={activate}\u003e\n      Activate\n    \u003c/button\u003e\n  );\n}\n```\n\nIf the value should be persisted to (and initialized from) local storage, use `StoredMemoryValue`:\n\n```typescript\nexport const MY_STATE = new StoredMemoryValue\u003cState\u003e('local.key.name');\n```\n\n## TypeScript warnings\n\nIt is _important_ to use `type` and not `interface` when using this in conjunction with TypeScript.\nThe 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.\n\nYou want this because non-serializable fields would be lost during serialization/deserialization, and thus can cause run-time issues.\n\nIf you get errors that the type is not `Serializable`, make sure you're only using `type` and not `interface`.\n\n## Android/iOS secure storage\n\nSwap `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**.\n\nNote: There are limitations to storage size.\n\n## Web configuration\n\nFor the web it uses `localforage` under the hood. You can set the instance yourself by importing\n\n```typescript\nimport { setLocalForageInstance } from 'expo-use-memory-value/storage.web';\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleeplessbyte%2Fexpo-use-memory-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsleeplessbyte%2Fexpo-use-memory-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleeplessbyte%2Fexpo-use-memory-value/lists"}