Skip to content

Registry for most popular React hooks based on the shadcn/ui.

License

Notifications You must be signed in to change notification settings

letstri/hookas

Repository files navigation

Hookas

Hookas is a comprehensive registry for popular React hooks, inspired by the shadcn registry system. It provides a collection of well-tested, production-ready hooks that solve common React development challenges.

How to use

Find the hook you want to use and copy the link to install the hook into your project. Please note you should have setup shadcn in your project to use this.

Hooks

useIsOnline

Check if the user is online.

Usage

import { useIsOnline } from '@/hookas/use-is-online'

function ConnectionStatus() {
  const isOnline = useIsOnline()
  return <div>{isOnline ? 'Online' : 'Offline'}</div>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-is-online.json

useAsyncEffect

Run an async effect.

Usage

import { useAsyncEffect } from '@/hookas/use-async-effect'
import { useState } from 'react'

function DataFetcher() {
  const [data, setData] = useState(null)

  useAsyncEffect(async () => {
    const res = await fetch('https://api.example.com/data')
    const json = await res.json()
    setData(json)
  }, [])

  return <pre>{JSON.stringify(data, null, 2)}</pre>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-async-effect.json

useElementSize

Measure the size of an element.

Usage

import { useElementSize } from '@/hookas/use-element-size'
import { useRef } from 'react'

function ResizableBox() {
  const ref = useRef(null)
  const { width, height } = useElementSize(ref)

  return (
    <div ref={ref}>
      <p>
        Width:
        {width}
        px
      </p>
      <p>
        Height:
        {height}
        px
      </p>
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-element-size.json

useClickOutside

Handle click outside events.

Usage

import { useClickOutside } from '@/hookas/use-click-outside'
import { useRef, useState } from 'react'

function DropdownMenu() {
  const [isOpen, setIsOpen] = useState(false)
  const ref = useRef(null)

  useClickOutside(ref, () => setIsOpen(false))

  return (
    <div ref={ref}>
      <button onClick={() => setIsOpen(!isOpen)}>Toggle Menu</button>
      {isOpen && <div>Menu Content</div>}
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-click-outside.json

useToggle

Toggle a value.

Usage

import { useToggle } from '@/hookas/use-toggle'

function ToggleButton() {
  const [isOn, toggle] = useToggle(false)

  return (
    <div>
      <button onClick={toggle}>{isOn ? 'ON' : 'OFF'}</button>
      <span>
        State:
        {isOn ? 'Enabled' : 'Disabled'}
      </span>
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-toggle.json

useWindowSize

Get the size of the window.

Usage

import { useWindowSize } from '@/hookas/use-window-size'

function WindowSizeDisplay() {
  const { width, height } = useWindowSize()

  return (
    <div>
      <p>
        Width:
        {width}
        px
      </p>
      <p>
        Height:
        {height}
        px
      </p>
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-window-size.json

useIsMounted

Check if the component is mounted.

Usage

import { useIsMounted } from '@/hookas/use-is-mounted'
import { useEffect, useState } from 'react'

function MountStatus() {
  const isMounted = useIsMounted()

  return (
    <div>
      Component is
      {isMounted ? 'mounted' : 'not mounted'}
      !
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-is-mounted.json

useQuery

Small alternative to @tanstack/react-query.

Usage

import { useQuery } from '@/hookas/use-query'

function DataFetcher() {
  const { data, error, status, refetch } = useQuery(() => fetch('https://api.example.com/data'))

  return <div>{JSON.stringify(data)}</div>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-query.json

useMediaQuery

Check if the browser matches a media query.

Usage

import { useMediaQuery } from '@/hookas/use-media-query'

function MediaQueryExample() {
  const isMobile = useMediaQuery('(max-width: 768px)')

  return <div>{isMobile ? 'Mobile' : 'Desktop'}</div>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-media-query.json

useFullscreen

Handle fullscreen mode.

Usage

import { useFullscreen } from '@/hookas/use-fullscreen'

function FullscreenExample() {
  const { isFullscreen, toggleFullscreen } = useFullscreen()
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-fullscreen.json

useMousePosition

Track the mouse position.

Usage

import { useMousePosition } from '@/hookas/use-mouse-position'

function MousePosition() {
  const { x, y } = useMousePosition()
}

function MousePosition() {
  const ref = useRef(null)
  const { x, y } = useMousePosition(ref)

  return (
    <div ref={ref}>
      {x}
      {y}
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-mouse-position.json

useDebouncedCallback

Debounce a callback.

Usage

import { useDebouncedCallback } from '@/hookas/use-debounced-callback'

function DebouncedCallback() {
  const debouncedFn = useDebouncedCallback((a: number, b: number) => {
    console.log(a, b)
  }, 1000)

  return <button onClick={() => debouncedFn(1, 2)}>Debounce</button>
}

useDebouncedMemo

Debounce a memo.

Usage

import { useDebouncedMemo } from '@/hookas/use-debounced-memo'

function DebouncedMemo() {
  const debouncedMemo = useDebouncedMemo(() => 'Hello', [1, 2, 3], 1000)

  return <div>{debouncedMemo}</div>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-debounced-memo.json

useDebouncedState

Debounce a state.

Usage

import { useDebouncedState } from '@/hookas/use-debounced-state'

function DebouncedState() {
  const [debouncedState, state, setState] = useDebouncedState('Hello', 1000)
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-debounced-state.json

useThrottledCallback

Throttle a callback.

Usage

import { useThrottledCallback } from '@/hookas/use-throttled-callback'

function ThrottledCallback() {
  const throttledFn = useThrottledCallback((a: number, b: number) => {
    console.log(a, b)
  }, 1000)

  return <button onClick={() => throttledFn(1, 2)}>Throttle</button>
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-throttled-callback.json

usePromise

Handle promises without use hook.

Usage

import { usePromise } from '@/hookas/use-promise'

function PromiseExample() {
  const data = usePromise(async () => [{ name: 'Valerii' }], [])

  return (
    <div>
      {data.map(item => (
        <div key={item.name}>{item.name}</div>
      ))}
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-promise.json

useMediaControls

Control media elements.

Usage

import { useMediaControls } from '@/hookas/use-media-controls'

function MediaControls() {
  const mediaRef = useRef<HTMLVideoElement>(null)
  const {
    play,
    pause,
    toggle,
    stop,
    toggleMute,
    setVolume,
    setCurrentTime,
    isPlaying,
    isMuted,
    volume,
    currentTime,
    duration,
  } = useMediaControls(mediaRef)

  return (
    <div>
      <video ref={mediaRef} src="src/assets/video.mp4" />
      <button onClick={play}>Play</button>
      <button onClick={pause}>Pause</button>
      <button onClick={toggle}>Toggle</button>
    </div>
  )
}

Install

npx shadcn@latest add https://hookas.letstri.dev/r/use-media-controls.json

About

Registry for most popular React hooks based on the shadcn/ui.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project