{"id":20672721,"url":"https://github.com/nas5w/use-local-storage","last_synced_at":"2025-04-05T11:08:42.369Z","repository":{"id":41160263,"uuid":"328171644","full_name":"nas5w/use-local-storage","owner":"nas5w","description":"A flexible React Hook for using Local Storage.","archived":false,"fork":false,"pushed_at":"2023-07-23T03:32:57.000Z","size":531,"stargazers_count":126,"open_issues_count":6,"forks_count":16,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-29T10:05:57.395Z","etag":null,"topics":["javascript","local-storage","react","react-hooks","typescript"],"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/nas5w.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","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-01-09T14:20:03.000Z","updated_at":"2025-03-10T18:16:36.000Z","dependencies_parsed_at":"2024-06-18T13:55:07.136Z","dependency_job_id":"6595bffc-b49c-4e95-9e18-1b9d57774a65","html_url":"https://github.com/nas5w/use-local-storage","commit_stats":{"total_commits":21,"total_committers":4,"mean_commits":5.25,"dds":"0.23809523809523814","last_synced_commit":"92064ca0d201ba578ba29e6cdfe96bf1230732bd"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nas5w%2Fuse-local-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nas5w%2Fuse-local-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nas5w%2Fuse-local-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nas5w%2Fuse-local-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nas5w","download_url":"https://codeload.github.com/nas5w/use-local-storage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325693,"owners_count":20920714,"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":["javascript","local-storage","react","react-hooks","typescript"],"created_at":"2024-11-16T20:38:36.677Z","updated_at":"2025-04-05T11:08:42.350Z","avatar_url":"https://github.com/nas5w.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"![useLocalStorage React hook](https://raw.githubusercontent.com/nas5w/use-local-storage/master/uls-logo.png)\n\nA flexible React Hook for using Local Storage.\n\n![Test Status](https://github.com/nas5w/use-local-storage/actions/workflows/test.yml/badge.svg) [![Codecov Status](https://codecov.io/gh/nas5w/use-local-storage/branch/master/graph/badge.svg)](https://codecov.io/gh/nas5w/use-local-storage/branch/master)\n\n\u003chr /\u003e\n\n![useLocalStorage React hook demo](https://raw.githubusercontent.com/nas5w/use-local-storage/master/uls-demo.gif)\n\n\u003chr /\u003e\n\n# Features\n\n✅ Persists data to local storage with an interface similar to the React `useState` hook\n\n✅ Works with any hooks-compatible React version\n\n✅ Works with SSR\n\n✅ Syncs data between components in the same or different browser tabs\n\n# Installation\n\nInstall with npm\n\n```bash\nnpm i use-local-storage\n```\n\nInstall with yarn\n\n```bash\nyarn add use-local-storage\n```\n\n# Basic Usage\n\nIn its most basic form, the `useLocalStorage` hook just needs the Local Storage `key` you wish to use. However, it's advised that you also provde a default value as a second argument in the event that they `key` does not yet exist in Local Storage.\n\nThe following usage will persist the `username` variable in a `\"name\"` key in Local Storage. It will have a default/initial value of an empty string `\"\"`. This default value will _only_ be used if there is no value already in Local Storage, moreover setting the variable `username` to `undefined` will remove it from Local Storage.\n\n```jsx\nimport useLocalStorage from \"use-local-storage\";\n\nfunction MyComponent() {\n  const [username, setUsername] = useLocalStorage(\"name\", \"\");\n\n  return (\n    \u003c\u003e\n      \u003cinput\n        value={username}\n        onChange={(e) =\u003e {\n          setUsername(e.target.value);\n        }}\n      /\u003e\n      \u003cbutton\n        onClick={() =\u003e {\n          setUsername(undefined);\n        }}\n      \u003e\n        Remove Username\n      \u003c/button\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n**Note:** By default, the `useLocalStorage` hook uses `JSON.stringify` and `JSON.parse` to serialize and parse the data, respectively. A custom serializer and/or parser can be configured if desired (discussed below in the Advanced Usage section).\n\n## Typescript Use\n\nThe type of `username` will be inferred from your default value. In this case, the type of `string` will be inferred.\n\nIf you use `useLocalStorage` _without_ providing a default value, or you simply want to specify that `username` is actually of a different type, you should pass the type of your data as a generic:\n\n```tsx\nimport useLocalStorage from \"use-local-storage\";\n\nfunction MyComponent() {\n  const [username, setUsername] = useLocalStorage\u003cstring\u003e(\"name\");\n\n  return (\n    \u003c\u003e\n      \u003cinput\n        value={username}\n        onChange={(e) =\u003e {\n          setUsername(e.target.value);\n        }}\n      /\u003e\n      \u003cbutton\n        onClick={() =\u003e {\n          setUsername(undefined);\n        }}\n      \u003e\n        Remove Username\n      \u003c/button\u003e\n    \u003c/\u003e\n  );\n}\n```\n\n# Advanced Usage / Options\n\nthe `useLocalStorage` hook takes an optional third `options` argument. This allows you to configure a custom serializer and/or parser if you need to use something other than `JSON.stringify` and `JSON.parse`. The `options` object also has a `logger` key to log an errors caught in the hook. You can also disable the cross-context synchronization by setting `syncData` to false.\n\n```javascript\nconst options = {\n  serializer: (obj) =\u003e {\n    /* Serialize logic */\n    return someString;\n  },\n  parser: (str) =\u003e {\n    /* Parse logic */\n    return parsedObject;\n  },\n  logger: (error) =\u003e {\n    // Do some logging\n  },\n  syncData: false, // You can disable cross context sync\n};\n\nconst [data, setData] = useLocalStorage(\"data\", { foo: \"bar\" }, options);\n```\n\n# Attribution\n\n\u003cdiv\u003eStorage icon made by \u003ca href=\"https://www.flaticon.com/authors/dinosoftlabs\" title=\"DinosoftLabs\"\u003eDinosoftLabs\u003c/a\u003e from \u003ca href=\"https://www.flaticon.com/\" title=\"Flaticon\"\u003ewww.flaticon.com\u003c/a\u003e\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnas5w%2Fuse-local-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnas5w%2Fuse-local-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnas5w%2Fuse-local-storage/lists"}