{"id":4479,"url":"https://github.com/visuallylab/react-native-use-persist-storage","last_synced_at":"2025-08-04T01:32:30.381Z","repository":{"id":34922665,"uuid":"190324818","full_name":"visuallylab/react-native-use-persist-storage","owner":"visuallylab","description":"Persist and rehydrate a context store by React Hooks","archived":false,"fork":false,"pushed_at":"2023-01-27T04:27:34.000Z","size":485,"stargazers_count":39,"open_issues_count":14,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-29T04:58:44.841Z","etag":null,"topics":["hooks","persistent-storage","react-native","react-native-hooks","storage","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/visuallylab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-05T04:16:56.000Z","updated_at":"2024-11-04T13:28:45.000Z","dependencies_parsed_at":"2023-02-15T05:16:39.759Z","dependency_job_id":null,"html_url":"https://github.com/visuallylab/react-native-use-persist-storage","commit_stats":null,"previous_names":["visuallylab/react-native-usepersiststorage"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/visuallylab/react-native-use-persist-storage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/visuallylab%2Freact-native-use-persist-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/visuallylab%2Freact-native-use-persist-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/visuallylab%2Freact-native-use-persist-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/visuallylab%2Freact-native-use-persist-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/visuallylab","download_url":"https://codeload.github.com/visuallylab/react-native-use-persist-storage/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/visuallylab%2Freact-native-use-persist-storage/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268636393,"owners_count":24282083,"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","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","persistent-storage","react-native","react-native-hooks","storage","typescript"],"created_at":"2024-01-05T20:17:13.828Z","updated_at":"2025-08-04T01:32:30.079Z","avatar_url":"https://github.com/visuallylab.png","language":"TypeScript","funding_links":[],"categories":["Components"],"sub_categories":["Utils \u0026 Infra"],"readme":"# React Native Hooks - usePersistStorage\n\nPersist and rehydrate a **context store** by React Hooks\n\n- An asynchronous persist storage\n- Support **sensitive info** both on iOS \u0026 Android\n- Migration function\n\n[![npm version](https://badge.fury.io/js/react-native-use-persist-storage.svg)](https://badge.fury.io/js/react-native-use-persist-storage)\n\n## Install\n\nInstall react-native-use-persist-storage\n\n```\n$ yarn add react-native-use-persist-storage\n```\n\nInstall **@react-native-async-storage/async-storage**, **react-native-sensitive-info**\n(see [peerDependencies](https://github.com/visuallylab/react-native-use-persist-storage#peer-dependencies))\n\n```\n$ yarn add react-native-sensitive-info @react-native-async-storage/async-storage\n```\n\nIf RN \u003c 0.60, link dependencies\n\n```\n$ react-native link react-native-sensitive-info\n$ react-native link @react-native-async-storage/async-storage\n```\n\nNote: For IOS project using pods, run: \\$ cd ios/ \u0026\u0026 pod install\n\n#### Peer Dependencies\n\n**Note:** `react-native-use-persist-storage` requires the following peer dependencies:\n\n- react \u003e= 16.8.1\n- react-native \u003e= 0.59.0\n- [react-native-sensitive-info](https://github.com/mCodex/react-native-sensitive-info)\n- [@react-native-async-storage/async-storage](https://github.com/react-native-async-storage/async-storage)\n\n---\n\n## Usage\n\nMake sure you know how [React Hooks](https://reactjs.org/docs/hooks-reference.html) works, and you can use it just same as useState.\n\n#### Basic Usage\n\n```js\n// User.js\nimport { usePersistStorage } from \"react-native-use-persist-storage\";\n\nconst createDefaultUser = () =\u003e ({\n  name: \"\"\n});\n\nconst User = props =\u003e {\n  const [user, setUser, restored] = usePersistStorage(\n    \"@User\",\n    createDefaultUser\n  );\n  if (restored) return \u003cText\u003eloading...\u003c/Text\u003e;\n  return \u003cText\u003e{user.name}\u003c/Text\u003e;\n};\n```\n\n#### Context Usage\n\nIf you want a light-weight global state management solution in react, try using [Context](https://reactjs.org/docs/context.html).\n\n```js\n// contexts/user.js\nimport { createContext } from 'react'\nimport { usePersistStorage } from 'react-native-use-persist-storage'\n\nconst createDefaultUser = () =\u003e ({\n  name: '',\n});\n\nconst UserContext = createContext(...);\n\nconst UserProvider = props =\u003e {\n  const [user, setUser, restored] = usePersistStorage('@User', createDefaultUser);\n\n  // anything you need...\n\n  return (\n    \u003cUserContext.Provider value={\n      { user, updateUser: setUser, restored }\n      // or like this\n      // [ user, updateUser: setUser, restored ]\n    }\u003e\n      {props.children}\n    \u003c/UserContext.Provider\u003e\n  );\n};\n```\n\n```js\n// GlobalStateProvider.ts\nconst GlobalStateProvider = ({ children }) =\u003e (\n  \u003cOtherContextProvider\u003e\n    \u003cUserProvider\u003e\n      {children}\n    \u003c/UserProvider\u003e\n  \u003c/OtherContextProvider\u003e\n)\n\n// App.js\nconst App = () =\u003e {\n  return (\n    \u003cGlobalStateProvider\u003e\n      \u003cRoot /\u003e\n    \u003c/GlobalStateProvider\u003e\n  );\n};\n```\n\n#### Recommend use: [createPersistContext](#createPersistContext)\n\n## API\n\n#### `usePersistStorage(key, initialValue, options?)`\n\n- arguments\n  - `key`: async storage key\n  - `initialValue`: initial value\n  - `options`: set options `debug`, `persist`, `version`, `migrate`, `sensitive`.\n\n##### `options` ([see](https://github.com/visuallylab/react-native-use-persist-storage/blob/master/src/defaultOptions.ts#L4))\n\n- `debug`: call `console.debug` when any persist storage action.\n  - default: `false`\n- `persist`: set false, it will same as useState\n  - default: `true`\n- `version`: storage version, set with migrate function\n  - default: `0`\n- `migrate`: set migrate function, [see how to use createMigrate](#createMigrate)\n  - default: `null`\n- `sensitive`: pass [config options](https://mcodex.dev/react-native-sensitive-info/docs/5.x/ios_options), it will use [react-native-sensitive-info](https://github.com/mCodex/react-native-sensitive-info) to store your data.\n  - default: `false`\n\n#### `createPersistContext`\n\nIt is a simple built-in util function for easy use. [See](https://github.com/visuallylab/react-native-use-persist-storage/blob/master/src/createPersistContext.tsx)\n\n```js\n// contexts/user.js\nimport { createPersistContext } from 'react-native-use-persist-storage';\n\nexport default createPersistContext({\n  storageKey: '@User',\n  defaultData: {\n    name: 'Awesome!'\n  },\n  options: { ... }\n});\n\n// App.js\nimport User from './contexts/user';\nconst App = () =\u003e {\n  return (\n    \u003cUser.Provider\u003e\n      \u003cRoot /\u003e\n    \u003c/User.Provider\u003e\n  );\n};\n\n// Component.js\nimport User from './contexts/user';\n\nconst Component = () =\u003e {\n  const [user, setUser] = User.useData();\n  return ...\n};\n\n```\n\n**Or, you can create a hook to customize**\n\n```js\n// hooks/useUser.js\nimport User from './contexts/user';\n\nconst useUser = () =\u003e {\n  const [user, setUser] = useContext(User.Context);\n  const setName = () =\u003e ...\n  const setEmail = () =\u003e ...\n  return {\n    user,\n    setName,\n    setEmail,\n    setUser,\n    // anything ...\n  };\n};\n\nconst Component = () =\u003e {\n  const { user, ... } = useUser();\n  ...\n}\n\n```\n\n#### `createMigrate`\n\nUse like [redux migration](https://github.com/rt2zz/redux-persist/blob/master/docs/migrations.md)\n\n```js\nimport { createMigrate, usePersistStorage } from 'react-native-use-persist-storage';\n\nconst [user, setUser, restored] = usePersistStorage(\n  '@User',\n  defaultUser,\n  {\n    version: 2,\n    migrate: createMigrate(\n      {\n        1: state =\u003e {\n          // ...\n        },\n        2: state =\u003e ({\n          // ...\n        }),\n        \n      },\n    ),\n  },\n)\n\n```\n\n#### `setPersistStorageDefaults`\nYou can control all default options when app initialized.([see](https://github.com/visuallylab/react-native-use-persist-storage/blob/master/src/defaultOptions.ts#L4))\n\n- open debug log:\n```js\nsetPersistStorageDefaults({ debug: true });\n```\n\n- remove all stored values in persistent storage:\n```js\nsetPersistStorageDefaults({ persist: false });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvisuallylab%2Freact-native-use-persist-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvisuallylab%2Freact-native-use-persist-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvisuallylab%2Freact-native-use-persist-storage/lists"}