{"id":28376365,"url":"https://github.com/robinbobin/react-native-preferences","last_synced_at":"2026-02-05T15:33:46.832Z","repository":{"id":134357120,"uuid":"349979307","full_name":"RobinBobin/react-native-preferences","owner":"RobinBobin","description":"A persistent unencrypted storage for application preferences.","archived":false,"fork":false,"pushed_at":"2024-12-24T03:46:01.000Z","size":331,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-30T00:06:34.897Z","etag":null,"topics":["async-storage","react-native"],"latest_commit_sha":null,"homepage":"","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/RobinBobin.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-21T11:34:04.000Z","updated_at":"2024-12-24T03:46:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"5f644157-c000-488e-b3d1-9b08cdb1f6eb","html_url":"https://github.com/RobinBobin/react-native-preferences","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/RobinBobin/react-native-preferences","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinBobin%2Freact-native-preferences","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinBobin%2Freact-native-preferences/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinBobin%2Freact-native-preferences/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinBobin%2Freact-native-preferences/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobinBobin","download_url":"https://codeload.github.com/RobinBobin/react-native-preferences/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinBobin%2Freact-native-preferences/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262047940,"owners_count":23250456,"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":["async-storage","react-native"],"created_at":"2025-05-30T00:06:39.070Z","updated_at":"2026-02-05T15:33:46.826Z","avatar_url":"https://github.com/RobinBobin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿A persistent unencrypted storage for application preferences.\n\n1. \u003ca name=\"cinstallation\"\u003e\u003c/a\u003e[Installation](#installation)\n2. \u003ca name=\"cusage\"\u003e\u003c/a\u003e[Usage](#usage)\n3. \u003ca name=\"cversionhistory\"\u003e\u003c/a\u003e[Version history](#versionhistory)\n\n### \u003ca name=\"installation\"\u003e\u003c/a\u003e[Installation](#cinstallation)\n\n    npm i @robinbobin/react-native-preferences\n    npm i @react-native-async-storage/async-storage@^1.14.1\n\nMy package uses [`@react-native-async-storage/async-storage`](https://www.npmjs.com/package/@react-native-async-storage/async-storage/v/1.14.1) to manage preference values, and that package needs linking. If it's specified as a dependency of `@robinbobin/react-native-preferences` it's not added to an app's `package.json` as a dependency and is not linked. Hence the need for manual installation.\n\n### \u003ca name=\"usage\"\u003e\u003c/a\u003e[Usage](#cusage)\n\nExample:\n\n    import {\n    \tStringPreference,\n    \tusePreferences\n    } from  \"@robinbobin/react-native-preferences\";\n\n    function  App() {\n    \t...\n    \tconst onLoad = useCallback(() =\u003e {\n    \t\tconsole.log(preferences.pref.name, preferences.pref.value);\n    \t\tpreferences.pref.value = \"peakaboo\";\n    \t\tconsole.log(preferences.pref.name, preferences.pref.value);\n    \t}, []);\n\n    \tconst preferences = usePreferences([\n    \t\tnew StringPreference(\"pref\", \"for you\")\n    \t], onLoad);\n    \t...\n    }\n\n\u003cbr\u003e\n\n1. \u003ca name=\"cpreferences\"\u003e\u003c/a\u003e[Preferences](#preferences)\n2. \u003ca name=\"cpreference\"\u003e\u003c/a\u003e[Preference](#preference)\n3. \u003ca name=\"cbooleanpreference\"\u003e\u003c/a\u003e[BooleanPreference](#booleanpreference)\n4. \u003ca name=\"cjsonpreference\"\u003e\u003c/a\u003e[JsonPreference](#jsonpreference)\n5. \u003ca name=\"cnumberpreference\"\u003e\u003c/a\u003e[NumberPreference](#numberpreference)\n6. \u003ca name=\"cstringpreference\"\u003e\u003ca\u003e[StringPreference](#stringpreference)\n7. \u003ca name=\"cusepreferences\"\u003e\u003c/a\u003e[usePreferences()](#usepreferences)\n\n#### \u003ca name=\"preferences\"\u003e\u003c/a\u003e[Preferences](#cpreferences)\n\nAn instance of this class stores all the preferences. See [`usePreferences()`](#usepreferences).\n\n- [areLoaded](#preferences)\n\n  A boolean property, returning `true` if the properties have been loaded and `false` otherwise.\n\n- \u003ca name=\"preferencesget\"\u003e\u003c/a\u003e[get()](#preferences)\n\n  Gets a preference by name. Throws an `Error` instance if the preferences haven't been loaded yet or if there's no preference with that name.\n\n  Generally it's easier to use the `preferences.preferenceName` syntax for the same purpose. This function can be used to access preferences with reserved names (names of properties / methods of this class and names starting with an underscore).\n\n- [load()](#preferences)\n\n  Loads the preferences. This function is invoked internally by [`usePreferences()`](#usepreferences).\n\n#### \u003ca name=\"preference\"\u003e\u003c/a\u003e[Preference](#cpreference)\n\nThis class serves as the base class for the classes that manage preference values ([`NumberPreference`](#numberpreference), [`StringPreference`](#stringpreference), etc). It shouldn't be instantiated directly.\n\n- [constructor()](#preference)\n\n  Takes 3 parameters:\n\n  - `name` \u0026ndash; The name of the preference. See also [`Preferences.get()`](#preferencesget).\n  - `defaultValue` \u0026ndash; A default value for the preference, used **only** on load if no value has been stored before.\n  - `valueTypes` \u0026ndash; Valid value types. Can be `undefined`, one type or an array of types.\n\n- \u003ca name=\"preferenceassertvalidity\"\u003e\u003c/a\u003e[assertValidity()](#preference)\n\n  Checks the validity of the passed value. If the value is deemed invalid, a `TypeError` is thrown.\n\n- [name](#preference)\n\n  A string property, returning this preference name.\n\n- [parse()](#preference)\n\n  Returns a value this preference can manage, being passed a string. The following must stand true:\n\n  - The parameter must be a valid string representation of the value.\n  - If the parameter is `null` this function must return `null`.\n\n- \u003ca name=\"preferencesave\"\u003e\u003c/a\u003e[save()](#preference)\n\n  Stores the preference value in a persistent storage. This method is invoked internally by the [`value` setter](#preferencevalue), but has to be called manually for \"compound\" preferences like [`JsonPreference`](#jsonpreference).\n\n- [stringify()](#preference)\n\n  Returns a string representation of the preference value.\n\n- [toString()](#preference)\n\n  Returns a human-readable representation of this preference.\n\n- \u003ca name=\"preferencevalue\"\u003e\u003c/a\u003e[value](#preference)\n\n  A getter / setter for the preference value. When setting a value, its validity is checked with [`assertValidity()`](#preferenceassertvalidity).\n\n#### \u003ca name=\"booleanpreference\"\u003e\u003c/a\u003e[BooleanPreference](#cbooleanpreference)\n\nA class to manage boolean values. The default value is `false`, if not specified.\n\n#### \u003ca name=\"jsonpreference\"\u003e\u003c/a\u003e[JsonPreference](#cjsonpreference)\n\nA class to manage object values (`{}`). The default value is an empty object, if not specified. Please, don't forget to call [`save()`](#preferencesave) when changing the object:\n\n    preferences.json.value.delay = 10;\n    preferences.json.save();\n\n#### \u003ca name=\"numberpreference\"\u003e\u003c/a\u003e[NumberPreference](#cnumberpreference)\n\nA class to manage number values. The default value is zero, if not specified.\n\n#### \u003ca name=\"stringpreference\"\u003e\u003c/a\u003e[StringPreference](#cstringpreference)\n\nA class to manage string values. The default value is an empty string, if not specified.\n\n#### \u003ca name=\"usePreferences\"\u003e\u003c/a\u003e[usePreferences()](#cusepreferences)\n\nThis function does the following:\n\n1. Creates an instance of [`Preferences`](#preferences), initializing it with the passed in `preferences`.\n2. Loads the preferences.\n3. Returns the created instance.\n\nThe return value of this function needn't be specified as a dependency of `React.useCallback()`, etc.\n\nThis function takes 3 parameters:\n\n- `preferences` \u0026ndash; an array of preferences (instances of classes derived from [`Preference`](#preference)).\n- `onLoad` \u0026ndash; a callback that is invoked when the preferences are loaded. It can return a clean-up function or a `Promise`, resolving to a clean-up function. In this case this clean-up function will be invoked **instead of** `onUnload()`.\n- `onUnload` \u0026ndash; (optional) a callback that is invoked on unmount.\n\n### \u003ca name=\"versionhistory\"\u003e\u003c/a\u003e[Version history](#cversionhistory)\n\n| Version number | Changes                                                                                                                                                                                                        |\n| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| v1.4.0         | [`usePreferences()`](#usePreferences): `onLoad()` can return a clean-up function, invoked **instead of** `onUnload()`.                                                                                         |\n| v1.3.0         | [`BooleanPreference`](#booleanpreference) added.                                                                                                                                                               |\n| v1.2.0         | 1. [`usePreferences()`](#usepreferences): `onUnload` added.\u003cbr\u003e2. Default values for [`JsonPreference`](#jsonpreference), [`NumberPreference`](#numberpreference) and [`StringPreference`](#stringpreference). |\n| v1.1.0         | 1. [`JsonPreference`](#jsonpreference) added.\u003cbr\u003e2. [`Preference.save()`](#preferencesave) added.                                                                                                              |\n| v1.0.3         | Initial release.                                                                                                                                                                                               |\n\n\u003cbr\u003e\u003cbr\u003e\n\n\u003e Written with [StackEdit](https://stackedit.io/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinbobin%2Freact-native-preferences","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinbobin%2Freact-native-preferences","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinbobin%2Freact-native-preferences/lists"}