@alevy97/react-native-userdefaults

中文文档
0.2.2发布于 2024/08/14MITTypeScriptCommonJS零依赖近 30 天下载 5193

UserDefaults in React Native

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Standard Defaults
  4. Defaults with Suite Name
  5. Example
  6. API

UserDefaults

An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app. Inspired by UserDefaults.

  • :white_check_mark: Built with Expo's Module API
  • :white_check_mark: Written in TypeScript and Swift
  • :apple: Currently iOS only

Installation

  1. Install the package
yarn add @alevy97/react-native-userdefaults
  1. Install pods
npx pod-install

Usage

Standard Defaults

The most common use case for using UserDefaults is to store small chunks of data to the standard defaults system. You can access the standard defaults system using the static property UserDefaults.standard or by creating a new UserDefaults instance.

UserDefaults.standard;

or equivalently

new UserDefaults();

Defaults with Suite Name

UserDefaults can also be used to store data that can be shared across multiple apps belonging to the same App Group, also known as a suite. To achieve this, provide a suite name (app group identifier) when creating a UserDefaults instance.

new UserDefaults("group.com.example.app");

Example

import { Button, View } from "react-native";
import UserDefaults from "@alevy97/react-native-userdefaults";

const standardDefaults = UserDefaults.standard; // or new UserDefaults()
const groupDefaults = new UserDefaults("group.com.example.app");

function App() {
  return (
    <View>
      <Button
        title="Set Values"
        onPress={async () => {
          await standardDefaults.set("darkMode", true);
          await groupDefaults.set("coolNumbers", [3.14, 2.71, 1.41]);
        }}
      />
      <Button
        title="Get Values"
        onPress={async () => {
          const darkMode = await standardDefaults.get("darkMode");
          const coolNumbers = await groupDefaults.get("coolNumbers");
          console.log(darkMode, coolNumbers);
        }}
      />
    </View>
  );
}

API

interface UserDefaults {
  /* Standard user defaults object */
  standard: UserDefaults;
  /* Get the value for a given key */
  get(forKey: string): Promise<any>;
  /* Set a value for a given key */
  set(forKey: string, value: any): Promise<void>;
  /* Remove a value for a given key */
  remove(forKey: string): Promise<void>;
  /* Removes all user default values */
  removeAll(): Promise<void>;
  /* Returns all user default values */
  getAll(): Promise<{ [key: string]: any }>;
}

版本与发布记录

@alevy97/react-native-userdefaults 共发布 5 个版本,首次发布于 2022/04/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.2latest2024/08/142 年前
0.2.12022/11/213 年前
0.2.02022/09/134 年前
0.1.12022/04/124 年前
0.1.02022/04/124 年前

关于 @alevy97/react-native-userdefaults 的常见问题

@alevy97/react-native-userdefaults 是什么?

UserDefaults in React Native

如何安装 @alevy97/react-native-userdefaults?

在项目目录运行 npm install @alevy97/react-native-userdefaults,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:expo(*)、react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alevy97/react-native-userdefaults 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alevy97/react-native-userdefaults 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@alevy97/react-native-userdefaults 可以商用吗?

@alevy97/react-native-userdefaults 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alevy97/react-native-userdefaults 的下载量有多少?

根据 npm 官方下载统计,@alevy97/react-native-userdefaults 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,193 次,日均约 173 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alevy97/react-native-userdefaults 还在维护吗?

最新版本 0.2.2 发布于 2024/08/14(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/04/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。