Skip to main content

Hooks

@rtorcato/react-common ships a set of small, headless React hooks — no UI, no extra dependencies. Import them from the package root:

import { usePrevious, useToggle } from '@rtorcato/react-common'

Available hooks

HookPurpose
useClickOutsideFire a callback when a click lands outside a ref.
useDebounceDebounce a rapidly-changing value.
useLocalStorageState synced to localStorage.
useMediaQueryTrack a CSS media query.
useIsMobiletrue below the mobile breakpoint.
useSidebarSidebar open/collapsed state.
usePreviousThe value from the previous render.
useToggleBoolean state with toggle/set helpers.

usePrevious

Returns the value from the previous render — undefined on the first render.

function Counter({ count }: { count: number }) {
const prev = usePrevious(count)
return <span>Now {count}, was {prev ?? '—'}</span>
}

useToggle

Boolean state with stable toggle / setOn / setOff helpers — the common open/closed, on/off pattern.

function Panel() {
const { value: isOpen, toggle, setOff } = useToggle()
return (
<>
<button onClick={() => toggle()}>Toggle</button>
{isOpen && <div onMouseLeave={setOff}>Content</div>}
</>
)
}

toggle() flips the value; toggle(true) / toggle(false) set it explicitly.