@custom-react-hooks/use-permission

中文文档
1.5.2发布于 2025/12/11MITTypeScriptCommonJS零依赖近 30 天下载 2168

usePermission is a React hook designed to query and monitor the status of user permissions for various browser APIs such as geolocation, notifications, microphone, and camera.

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Installing Only Current Hooks
  4. Installing All Hooks
  5. Importing the Hook
  6. Usage
  7. API Reference
  8. Parameters
  9. Supported Permissions
  10. Returns
  11. Use Cases
  12. Contributing

usePermission Hook

usePermission is a React hook designed to query and monitor the status of user permissions for various browser APIs such as geolocation, notifications, microphone, and camera.

Features

  • Permission Querying: Queries the status of a specified permission.
  • Real-time Updates: Monitors and updates the permission status in real-time if it changes.
  • Error Handling: Provides error messages if the Permissions API is not supported or if an error occurs during the query.
  • SSR Compatibility: Executes safely in a server-side rendering environment by avoiding direct browser API calls during SSR.

Installation

Installing Only Current Hooks

npm install @custom-react-hooks/use-permission

or

yarn add @custom-react-hooks/use-permission

Installing All Hooks

npm install @custom-react-hooks/all

or

yarn add @custom-react-hooks/all

Importing the Hook

The usePermission hook must be imported using a named import as shown below:

Named Import:

import { usePermission } from '@custom-react-hooks/use-permission';

This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.

Usage

import React, { useState } from 'react';
import { usePermission } from '@custom-react-hooks/all';

const PermissionComponent = () => {
  const [selectedPermission, setSelectedPermission] = useState('geolocation');
  const { state, isLoading, error } = usePermission(selectedPermission);

  return (
    <div>
      <h2>Check Browser Permission Status</h2>
      <label htmlFor="permission-select">Choose a permission: </label>
      <select
        id="permission-select"
        value={selectedPermission}
        onChange={(e) => setSelectedPermission(e.target.value)}
      >
        <option value="geolocation">Geolocation</option>
        <option value="notifications">Notifications</option>
        <option value="microphone">Microphone</option>
        <option value="camera">Camera</option>
        <option value="local-network-access">Local Network Access</option>
      </select>

      <div>
        {isLoading ? (
          <p>Loading...</p>
        ) : (
          <p>
            Permission status for {selectedPermission}:
            <span> {state}</span>
          </p>
        )}
        {error && <p>Error: {error}</p>}
      </div>
    </div>
  );
};

export default PermissionComponent;

API Reference

Parameters

  • permissionName: A string that represents the permission to query. It must be one of the supported permission names defined by the Permissions API.

Supported Permissions

  • geolocation - User's geographical location
  • notifications - Display notifications
  • push - Push notifications
  • microphone - Audio input access
  • camera - Video input access
  • speaker - Audio output access
  • device-info - Device information
  • background-sync - Background synchronization
  • bluetooth - Bluetooth device access
  • persistent-storage - Persistent storage
  • ambient-light-sensor - Ambient light sensor
  • accelerometer - Accelerometer sensor
  • gyroscope - Gyroscope sensor
  • magnetometer - Magnetometer sensor
  • clipboard-read - Read from clipboard
  • clipboard-write - Write to clipboard
  • payment-handler - Payment handler
  • local-network-access - Local network access (Chrome 142+)

Returns

  • state: A string representing the permission state ('prompt', 'granted', or 'denied').
  • isLoading: A boolean indicating if the permission query is in progress.
  • error: A string containing an error message if the query fails or if the Permissions API is not supported.

Use Cases

  • Feature Availability Checks: Check if a user has granted permission for features like geolocation, notifications, or camera access.
  • Conditional Feature Access: Enable or disable features based on permission status.
  • User Permission Management: Prompt users for necessary permissions or provide feedback on their status.
  • Privacy Compliance: Ensure compliance with privacy practices by checking permissions before accessing sensitive features.

Contributing

We welcome contributions to usePermission. Please report bugs or suggest feature enhancements through issues or pull requests in the project's repository.

版本与发布记录

@custom-react-hooks/use-permission 共发布 9 个版本,首次发布于 2023/12/02;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.5.2latest2025/12/1111 个月前
1.5.12024/08/182 年前
1.5.02024/08/182 年前
1.4.192023/12/092 年前
1.4.182023/12/082 年前
1.4.162023/12/072 年前

关于 @custom-react-hooks/use-permission 的常见问题

@custom-react-hooks/use-permission 是什么?

usePermission is a React hook designed to query and monitor the status of user permissions for various browser APIs such as geolocation, notifications, microphone, and camera.

如何安装 @custom-react-hooks/use-permission?

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

@custom-react-hooks/use-permission 支持 TypeScript 吗?

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

@custom-react-hooks/use-permission 支持 ESM 还是 CommonJS?

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

@custom-react-hooks/use-permission 可以商用吗?

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

@custom-react-hooks/use-permission 的下载量有多少?

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

@custom-react-hooks/use-permission 还在维护吗?

最新版本 1.5.2 发布于 2025/12/11(11 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/12/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。