npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-hook-useragent

v1.0.5

Published

A high performance user agent parser hook for React.

Downloads

14

Readme

React Hook UserAgent

npm version License: MIT React Version

A custom React hook for parsing and retrieving user agent information using the useragent library.

Overview

This hook leverages the browser's navigator.userAgent property to provide detailed information about the current browser, its version, operating system, and device. It memoizes the result to optimize performance in React applications.

Installation

Install the package via npm:

npm install react-hook-useragent

Since this package is a React hook, ensure that you have React installed in your project. This package also declares React as a peer dependency:

npm install react

Usage

After installation, import and use the hook in your React components:

import React from "react";
import useUserAgent from "react-hook-useragent";

function App() {
  const { browser, os } = useUserAgent();

  return (
    <div>
      <p>
        <strong>Browser:</strong> {browser}
      </p>
      <p>
        <strong>Operating System:</strong> {os}
      </p>
    </div>
  );
}

export default App;

How It Works

  • Retrieving the UA String:
    The hook accesses navigator.userAgent to obtain the browser's user agent information.

  • Performance Optimization:
    The hook uses useMemo to ensure that the parsing computation is performed only when the user agent string changes.

API

The hook returns an object representing the useragent. Extrapolate information such as browser, os, device, cpu, engine. Below is the Typescript interface for the return object

TypeScript Interface

interface UserAgentInfo {
  /**
   * The full User-Agent string.
   */
  ua: string;

  /**
   * Information about the browser.
   */
  browser: {
    /**
     * The name of the browser, e.g., "Chrome", "Firefox".
     */
    name: string;

    /**
     * The full version string of the browser.
     */
    version: string;

    /**
     * The major version number, often used for compatibility checks.
     */
    major: string;
  };

  /**
   * Information about the CPU.
   */
  cpu: {
    /**
     * CPU architecture, e.g., "amd64", "arm64".
     */
    architecture: string;
  };

  /**
   * Information about the device.
   * This is often empty for non-mobile devices.
   */
  device: {
    /**
     * Device model, if detected. May be absent on desktop platforms.
     */
    model?: string;

    /**
     * Device type, e.g., "mobile", "tablet", "desktop". May be absent on desktop platforms.
     */
    type?: string;

    /**
     * Device vendor, e.g., "Samsung", "Apple". May be absent on desktop platforms.
     */
    vendor?: string;
  };

  /**
   * Information about the rendering engine.
   */
  engine: {
    /**
     * The name of the rendering engine, e.g., "Blink", "Gecko".
     */
    name: string;

    /**
     * The version of the rendering engine.
     */
    version: string;
  };

  /**
   * Information about the operating system.
   */
  os: {
    /**
     * The name of the operating system, e.g., "Linux", "Windows".
     */
    name: string;

    /**
     * (Optional) The version of the operating system, if available.
     */
    version?: string;
  };
}

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License.