Skip to main content

usePermission

function usePermission(name: PermissionName | string): PermissionValue;

type PermissionValue = "granted" | "denied" | "prompt" | "pending" | "unsupported";

Asks the Permissions API for the state of a browser permission, such as "geolocation", "notifications", "camera" or "microphone", and re-renders when the user changes it in the browser's settings.

  • "prompt" means the browser will ask the user the next time the feature is used.
  • It is "pending" while the browser answers, during server rendering and on the first render while hydrating.
  • It is "unsupported" when the browser has no Permissions API or doesn't know name, as browsers support different permissions. Treat it like "prompt": try the feature.
  • When name changes it is "pending" again, and answers for the old name are ignored.

This hook only reads the state. To ask for a permission, use the feature itself, for example navigator.geolocation.getCurrentPosition.

Example​

import { usePermission } from "react-honey-hooks";

function LocationButton({ onLocate }: { onLocate: () => void }) {
const permission = usePermission("geolocation");

if (permission === "denied") {
return <p>Location is blocked. Allow it in your browser's site settings.</p>;
}
return <button onClick={onLocate}>Use my location</button>;
}