React Use Hooks Collection

17.6.0 · active · verified Sun Apr 19

react-use is a comprehensive, open-source collection of over 80 essential React Hooks, offering a wide array of utilities for managing state, effects, browser APIs, and sensor data. It aims to simplify common patterns and reduce boilerplate in React applications, providing modular and reusable solutions for tasks such as tracking window size, battery status, geolocation, and various input events. The library is actively maintained, with its current stable version being 17.6.0 (released December 2024), and demonstrates a frequent release cadence, often pushing minor updates and bug fixes multiple times a year. Its key differentiators include the sheer volume and diversity of well-documented hooks, strong TypeScript support, and a focus on abstracting complex browser APIs into simple hook interfaces. It originated as a port from the `libreact` library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `useWindowSize` to track browser dimensions, `useBattery` to monitor device battery status, and `useToggle` for simple boolean state management and conditional UI rendering within a React functional component.

import React from 'react';
import { useWindowSize, useBattery, useToggle } from 'react-use';

const App = () => {
  const { width, height } = useWindowSize();
  const batteryState = useBattery();
  const [isVisible, toggleVisibility] = useToggle(true);

  return (
    <div style={{ padding: '20px', fontFamily: 'sans-serif' }}>
      <h1>react-use Quickstart</h1>
      <p>Window Size: {width}px x {height}px</p>

      {batteryState.isSupported ? (
        <p>
          Battery Level: {((batteryState.level ?? 0) * 100).toFixed(0)}%
          {batteryState.charging ? ' (Charging)' : ' (Discharging)'}
        </p>
      ) : (
        <p>Battery status not supported on this device.</p>
      )}

      <button onClick={() => toggleVisibility()} style={{ marginTop: '15px', padding: '8px 12px', cursor: 'pointer' }}>
        {isVisible ? 'Hide' : 'Show'} Toggled Content
      </button>

      {isVisible && (
        <div style={{ marginTop: '15px', border: '1px dashed #ccc', padding: '10px', borderRadius: '4px' }}>
          <p>This content is toggled!</p>
          <p>Current Time: {new Date().toLocaleTimeString()}</p>
        </div>
      )}
    </div>
  );
};

export default App;

view raw JSON →