Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rooks

imbhargav5457.3kMIT9.3.0TypeScript support: included

Collection of awesome react hooks

react, hooks, custom, custom-hooks, typescript, react-hooks

readme




Why Rooks ?


CI and Semantic Release GitHub GitHub release (latest by date) GitHub contributors npm npm bundle size Codecov branch node-lts Website Module




Collection of awesome react hooks



List of all hooks

🎬 Animation & Timing - 5 hooks

  • useIntervalWhen - Sets an interval immediately when a condition is true
  • useLockBodyScroll - This hook locks the scroll of the body element when isLocked is set to true.
  • useRaf - A continuously running requestAnimationFrame hook for React
  • useResizeObserverRef - Resize Observer hook for React.
  • useTimeoutWhen - Takes a callback and fires it when a condition is true

🌐 Browser APIs - 11 hooks

  • useBroadcastChannel - A React hook that provides a clean interface to the Broadcast Channel API for cross-tab/window communication
  • useGeolocation - A hook to provide the geolocation info on client side.
  • useIdleDetectionApi - Hook to detect when user is idle using Idle Detection API with polyfill
  • useNavigatorLanguage - Navigator Language hook for React.
  • useOnline - Online status hook for React.
  • useOrientation - orientation hook for react
  • useScreenDetailsApi - Hook for multi-screen information and management using Screen Details API
  • useWebLocksApi - Hook for coordinating operations across tabs/workers with Web Locks API
  • useSpeech - Speech synthesis hook for React
  • useFetch - Hook for fetching data from URLs with loading states, error handling, and automatic JSON parsing
  • useVibrate - Vibration API hook for React

🛠️ Development & Debugging - 1 hook

🚀 Events - 15 hooks

📝 Form & File Handling - 2 hooks

  • useCheckboxInputState - Simple checkbox state management hook that provides a boolean state and props that can be spread directly onto a checkbox input element
  • useFileDropRef - Drop files easily

⌨️ Keyboard & Input - 5 hooks

  • useInput - Input hook for React.
  • useKey - keypress, keyup and keydown event handlers as hooks for react.
  • useKeyBindings - useKeyBindings can bind multiple keys to multiple callbacks and fire the callbacks on key press.
  • useKeyRef - Very similar useKey but it returns a ref
  • useKeys - A hook which allows to setup callbacks when a combination of keys are pressed at the same time.

🔥 Lifecycle & Effects - 9 hooks

🖱️ Mouse & Touch - 3 hooks

⚡ Performance & Optimization - 4 hooks

❇️ State - 18 hooks

  • useArrayState - Array state manager hook for React
  • useCountdown - Count down to a target timestamp and call callbacks every second (or provided peried)
  • useCounter - Counter hook for React.
  • useGetIsMounted - Checks if a component is mounted or not at the time. Useful for async effects
  • useLocalstorageState - UseState but auto updates values to localStorage
  • useMapState - A react hook to manage state in a key value pair map.
  • useMultiSelectableList - A custom hook to easily select multiple values from a list
  • useNativeMapState - Manage Map() object state in React
  • usePreviousDifferent - usePreviousDifferent returns the last different value of a variable
  • usePreviousImmediate - usePreviousImmediate returns the previous value of a variable even if it was the same or different
  • usePromise - Promise management hook for react
  • useQueueState - A React hook that manages state in the form of a queue
  • useSafeSetState - set state but ignores if component has already unmounted
  • useSelect - Select values from a list easily. List selection hook for react.
  • useSelectableList - Easily select a single value from a list of values. very useful for radio buttons, select inputs etc.
  • useSessionstorageState - useState but syncs with sessionstorage
  • useSetState - Manage the state of a Set in React.
  • useStackState - A React hook that manages state in the form of a stack

🔄 State History & Time Travel - 4 hooks

  • useTimeTravelState - A hook that manages state which can undo and redo. A more powerful version of useUndoState hook.
  • useToggle - Toggle (between booleans or custom data)hook for React.
  • useUndoRedoState - Setstate but can also undo and redo
  • useUndoState - Drop in replacement for useState hook but with undo functionality.

⚛️ UI - 13 hooks

  • useAudio - Audio hook
  • useBoundingclientrect - getBoundingClientRect hook for React.
  • useBoundingclientrectRef - A hook that tracks the boundingclientrect of an element. It returns a callbackRef so that the element node if changed is easily tracked.
  • useDimensionsRef - Easily grab dimensions of an element with a ref using this hook
  • useFullscreen - Use full screen api for making beautiful and emersive experinces.
  • useMeasure - Measures both inner and outer dimensions of any DOM element in a performant way and updates when dimensions change
  • useIntersectionObserverRef - A hook to register an intersection observer listener.
  • useInViewRef - Simple hook that monitors element enters or leave the viewport that's using Intersection Observer API.
  • useMediaMatch - Signal whether or not a media query is currently matched.
  • useMutationObserver - Mutation Observer hook for React.
  • useMutationObserverRef - A hook that tracks mutations of an element. It returns a callbackRef.
  • usePictureInPictureApi - Hook for managing Picture-in-Picture video functionality
  • useVideo - Video hook for react

🔧 Utilities & Refs - 7 hooks

  • useEventListenerRef - A react hook to add an event listener to a ref
  • useForkRef - A hook that can combine two refs(mutable or callbackRefs) into a single callbackRef
  • useFreshCallback - Avoid stale closures and keep your callback fresh
  • useFreshRef - Avoid stale state in callbacks with this hook. Auto updates values using a ref.
  • useFreshTick - Like use-fresh-ref but specifically for functions
  • useMergeRefs - Merges any number of refs into a single ref
  • useRefElement - Helps bridge gap between callback ref and state

📱 Window & Viewport - 2 hooks

🧪 Experimental Hooks - 5 hooks

⚠️ Experimental hooks may be removed or significantly changed in any release without notice. Use with caution in production.



Features

✅ Collection of 104 hooks as standalone modules.

✅ Standalone package with all the hooks at one place

✅ CommonJS, UMD and ESM Support

Installation

npm i -s rooks

Import any hook from "rooks" and start using them!

import { useDidMount } from "rooks";

Usage

function App() {
  useDidMount(() => {
    alert("mounted");
  });
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Standalone Package

Package containing all the hooks is over here. - Docs and Npm Install


License

MIT

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Bhargav Ponnapalli

💻 🚧

anil kumar chaudhary

💻

Qiwei Yang

💻 🚧

maciek_grzybek

💻

Harsh Zalavadiya

💻

B V K MAHIJENDRA

💻

Braxton Christensen

💻

Hansel

💻

Harshil Parmar

💻

Lionel

💻

Max Stoiber

💻

Michael Moore

💻

Rajas Paranjpe

💻

Mahendra Choudhary

💻

Nghia Pham

💻

Akshay Kadam (A2K)

💻

Alex Golubtsov

💻

Arman

💻

Branden Visser

💻

Brian Steere

💻

Cal Courtney

💻

Chris Milson

💻

Cong Zhang

💻

Daniel Holmes

💻

Fernando Beck

💻

Josh Davenport

💻

MARCEL

💻

Neilor Caldeira

💻

Tobias Lins

💻

Tsvetan

💻

Wei Zhu

💻

Yakko Majuri

💻

Frank Hellwig

💻

Austin Peterson

💻

thodubois

💻

wes christiansen

💻

CJ Patoilo

💻

mar1u50

💻

Ayushman Gupta

💻

Rafael Ferreira

💻

Kristinn Thor Johannsson

💻

Michael Moore

💻

Trevor Blades

💻

official_dulin

💻

Billy Mosis Priambodo

💻

Stafford Williams

💻

Chanhee Kim

💻

Hooriza

💻

Nils Wittler

💻

Sebastian Szczepański

💻

Mahendra Choudhary

💻

Som Shekhar Mukherjee

💻

Qiushi Pan

💻

Jishnu Viswanath

💻

brahambence

💻

dependabot[bot]

💻

renovate[bot]

💻

dependabot-preview[bot]

💻

github-actions[bot]

💻

allcontributors[bot]

💻

zhangenming

💻

Antoni Kiszka

💻

Greg Poole

💻

changelog

rooks

9.3.0

Minor Changes

9.2.0

Minor Changes

9.1.0

Minor Changes

9.0.2

Patch Changes

9.0.1

Patch Changes

9.0.0

Major Changes

Patch Changes

9.0.0-next.5

Patch Changes

9.0.0-next.4

Patch Changes

9.0.0-next.3

Patch Changes

9.0.0-next.2

Patch Changes

9.0.0-next.1

Patch Changes

9.0.0-next.0

Major Changes

8.4.0

Minor Changes

  • 6dc62d2 Thanks @imbhargav5! - - 926f423e: Added new React hooks to data/hooks-list.json.

    • a3e09c57: Fixed type aliasing in package build output using tsc-alias (packages/rooks).
    • a8eed151: Updated import paths in documentation for regular hooks to use relative paths.
    • ed4924f9: Refactored imports in index.ts to use relative paths for hooks.
    • 638d9d89: Added useSuspenseNavigatorBattery export and fixed JSON formatting.
    • 71c96694: Removed deprecated react-hook-yolo documentation file.
    • 46f33a87: Organized hooks in sidebar by category; updated multiple hook documentation files and metadata for improved structure.
    • 179aa4fc: Updated useGetIsMounted docs with comprehensive examples and use cases.
    • ed5321b8: Updated useArrayState docs with comprehensive examples and detailed API reference.
    • 425c6bb1: Updated useAsyncEffect docs with comprehensive examples and detailed explanations.
    • f1963051: General README.md update.

    Areas affected:

    • data/hooks-list.json
    • packages/rooks (build, package.json, index.ts, experimental.ts, tests)
    • apps/website/content/docs/hooks/ (many .mdx and meta.json files)
    • README.md
    • Scripts and documentation files

8.3.1

Patch Changes

8.3.0

Minor Changes

  • #1841 a70de17 Thanks @imbhargav5! - Enhance useAudio hook, add unit tests, and update documentation

    • useAudio Enhancement: Significantly enhanced the useAudio hook with improved functionality and comprehensive documentation (679 additions, 80 deletions across hook implementation, tests, and docs)
    • Unit Tests: Add unit tests for useBoundingclientrectRef, useFullscreen, useIntersectionObserverRef, useIsomorphicEffect, and useMutationObserverRef hooks
    • Documentation: Update README image source to use raw GitHub URL for better accessibility

    This minor release includes major improvements to the useAudio hook with new features, expanded test coverage for multiple hooks, and enhanced documentation accessibility.

Patch Changes

  • #1841 a70de17 Thanks @imbhargav5! - Add unit tests for several hooks and update README image source

    • Add unit tests for useBoundingclientrectRef, useFullscreen, useIntersectionObserverRef, useIsomorphicEffect, and useMutationObserverRef hooks
    • Update README image source to use raw GitHub URL for better accessibility

    This patch includes improvements to test coverage and documentation accessibility.

8.2.0

Minor Changes

  • #1835 825e69a Thanks @imbhargav5! - ## New Features and Improvements

    New Hooks

    • Add experimental useSuspenseNavigatorUserAgentData hook for enhanced user agent detection capabilities

    Bug Fixes and Enhancements

    • Fix useWebLocksApi hook to prevent combining its internal signal with the ifAvailable parameter, improving API reliability
    • Refactor useWindowSize hook for improved clarity and performance with additional tests
    • Enhance useAudio hook documentation for better clarity and examples

    Documentation Improvements

    • Update README image source to local path for better reliability
    • Refactor hooks list with improved categorization and markdown structure
    • Comprehensive documentation updates for multiple hooks including:
      • useUndoRedoState with API details and examples
      • useSetState with comprehensive examples and details
      • useRenderCount with use cases and examples
      • useOnClickRef with detailed explanations
      • useDeepCompareEffect with usage guide
      • useOnHoverRef with comprehensive examples
      • useWhyDidYouUpdate with detailed documentation
      • useSafeSetState with detailed explanations
      • useSessionstorageState with comprehensive examples
      • useDebounceFn with detailed explanations
    • Fix markdown code formatting in hook documentation tables
    • Add lucide-react dependency and update icon handling

    Developer Experience

    • Improved code formatting and structure across multiple hook documentations
    • Enhanced examples and use cases for better developer understanding
    • Better categorization and organization of hooks documentation

8.1.1

Patch Changes

  • 477133e Thanks @imbhargav5! - Rewrite useNavigatorLanguage and useDocumentVisibilityState to useSyncExternalStore

8.1.0

Minor Changes

8.0.1

Patch Changes

8.0.0

Major Changes

7.14.1

Patch Changes

7.14.0

Minor Changes

7.13.0

Minor Changes

  • 5b90f1b1 Thanks @imbhargav5! - useUndoRedoState: add isUndoPossible, isRedoPossible booleans to controls

7.12.0

Minor Changes

7.11.3

Patch Changes

7.11.2

Patch Changes

7.11.1

Patch Changes

7.11.0

Minor Changes

7.10.1

Patch Changes

7.10.0

Minor Changes

7.9.0

Minor Changes

7.8.5

Patch Changes

7.8.4

Patch Changes

7.8.3

Patch Changes

7.8.2

Patch Changes

7.8.1

Patch Changes

7.8.0

Minor Changes

  • 656e58e7 Thanks @imbhargav5! - added useMapObjectState, useSafeSetState, useDeepCompareEffect

7.7.0

Minor Changes

Patch Changes

7.6.1

Patch Changes

7.6.0

Minor Changes

  • #1583 ac0086dd Thanks @imbhargav5! - add useAudio, useVideo, useOnLongHover, useOnLongPress, useFreshCallback, useFileDropRef, useOrientation, useVibrate, useMouseMoveDelta, useMouseWheelDelta, useIsDroppingFiles hooks

7.5.0

Minor Changes

  • 7773cf47 Thanks @imbhargav5! - useArrayState initializer can be a function returning an array

7.4.4

Patch Changes

  • #1576 33b09d7a Thanks @imbhargav5! - useDebounce bug fixes. It will now work as intended with inline functions. Stale closure bug fixed

7.4.3

Patch Changes

7.4.2

Patch Changes

7.4.1

Patch Changes

7.4.0

Minor Changes

7.3.0

Minor Changes

  • f2ee3a66 Thanks @imbhargav5! - add splice, removeItemAtIndex and replaceItemAtIndex controls for useArrayState

7.2.1

Patch Changes

7.2.0

Minor Changes

7.1.2

Patch Changes

7.1.2-alpha.1

Patch Changes

7.1.2-alpha.0

Patch Changes

7.1.1

Patch Changes

  • bea1ddaf: fix isNumber import in usetimeTravelState

7.1.0

Minor Changes

  • 0dc2004e: New hook! useTimeTravelState hook added which can undo and redo a state variable seamlessly.

7.0.0

Major Changes

  • ae9a465a: useFullscreen hook rewrite

Patch Changes

  • b2003b87: bundle size decrease
  • 886a6721: Make useKey target option support undefined

7.0.0-alpha.2

Patch Changes

  • 886a6721: Make useKey target option support undefined

7.0.0-alpha.1

Patch Changes

  • b2003b87: bundle size decrease

6.4.3

Patch Changes

  • 140fceab: fix dependency checking in useAsyncEffect

7.0.0-alpha.0

Major Changes

  • 9ca2be66: useFullscreen hook rewrite

6.4.2

Patch Changes

  • 47e847ae: bugfix: window (or document) is not defined when using ssr

6.4.1

Patch Changes

  • 19b280fa: decrease bundle size

6.4.0

Minor Changes

  • 182e8498: add useArrayState and useSetState hooks to manage arrays and sets

6.3.0

Minor Changes

  • 2eac1e11: add useFocus & useFocusWithin hooks

6.2.1

Patch Changes

  • e3866ce5: useKeys comes with a new preventLostKeyup option for native events
  • 7aea2fea: Add an opt-in option to prevent useKeys from losing track of keyup.

6.2.0

Minor Changes

  • 11d5b33a: add useResizeObserverRef hook

6.1.0

Minor Changes

  • 33f6630d: add useAsyncEffect and useRenderCount hooks

6.0.1

Major Changes

  • 8b98cf06: useLocalstorage, useSessionstorage, useTimeout, useInterval, usePrevious, useVisibilitySensor, useUpdateEffect hooks removed. Please use useLocalstorageState, useSessionstorageState, useTimeoutWhen, useIntervalWhen, usePreviousImmediate, useInViewRef and useDidUpdate

6.0.0-alpha.0

Major Changes

  • a9d4e0af: useLocalstorage, useSessionstorage, useTimeout, useInterval, usePrevious, useVisibilitySensor, useUpdateEffect hooks removed. Please use useLocalstorageState, useSessionstorageState, useTimeoutWhen, useIntervalWhen, usePreviousImmediate, useInViewRef and useDidUpdate