@structyl/hooks
Hooks
17 reusable, SSR-safe, tree-shakeable React hooks. Zero dependencies. Import only what you use.
useBoolean
StateBoolean state with named semantic setters.
(initial?: boolean) => { value, on, off, toggle, set }Parameters
initialStarting value. Defaults to false.{ value: boolean, on: () => void, off: () => void, toggle: () => void, set: Dispatch }useToggle
StateBoolean state with a single toggle function.
(initial?: boolean) => [boolean, toggle, setValue]value: false
Parameters
initialStarting value. Defaults to false.[value: boolean, toggle: () => void, setValue: (v: boolean) => void]useCounter
StateNumeric counter with increment, decrement, and reset.
(initial?: number) => { count, increment, decrement, reset, set }Parameters
initialStarting count. Defaults to 0.{ count: number, increment: (by?: number) => void, decrement: (by?: number) => void, reset: () => void, set: Dispatch }usePrevious
StateReturns the value from the previous render cycle.
<T>(value: T) => T | undefinedPrevious
—
Current
0
Parameters
valueThe value to track.T | undefined — undefined on the first render.useDebounce
PerformanceDelays updating a value until after a quiet period. Ideal for search inputs.
<T>(value: T, delay?: number) => TParameters
valueThe rapidly changing value to debounce.delayMilliseconds to wait. Defaults to 300.T — the debounced value.useThrottle
PerformanceLimits how often a value updates to at most once per interval.
<T>(value: T, delay?: number) => TParameters
valueThe rapidly changing value to throttle.delayMinimum ms between updates. Defaults to 300.T — the throttled value.useLocalStorage
BrowserState that persists in localStorage and syncs across tabs.
<T>(key: string, initial: T) => [T, setValue, remove]Refresh the page — the value stays.
Parameters
keyThe localStorage key.initialFallback value when the key is absent.[value: T, setValue: (v: T | ((prev: T) => T)) => void, remove: () => void]useCopyToClipboard
BrowserCopies text to the clipboard with a timed copied state.
() => { copy, copied, reset }{ copy: (text: string) => Promise<boolean>, copied: boolean, reset: () => void }useMediaQuery
BrowserTracks any CSS media query. SSR-safe with a default value.
(query: string, defaultValue?: boolean) => booleanParameters
queryA valid CSS media query string.defaultValueReturned during SSR. Defaults to false.boolean — whether the query currently matches.useDarkMode
BrowserReturns true when the user prefers a dark color scheme.
() => booleanSystem preference: light
Change your OS color scheme to see this update.
boolean — true when prefers-color-scheme: dark.useWindowSize
BrowserTracks the browser viewport dimensions. SSR-safe.
() => { width: number, height: number }Width
0
px
Height
0
px
Resize the window to see values update live.
{ width: number, height: number }useClickOutside
DOMFires a handler when a pointer event lands outside the referenced element.
<T extends HTMLElement>(ref, handler, enabled?) => voidParameters
refRef attached to the element to watch.handlerCallback fired on outside click.enabledWhether to listen. Defaults to true.voiduseHotkeys
KeyboardBinds keyboard shortcut combinations. Supports Ctrl, Meta, Shift, Alt, and Mod (cross-platform).
(keys: string, handler, options?) => voidPress one of the combos above…
Parameters
keysCombo string, e.g. "mod+k" or "ctrl+shift+s".handlerCalled when the combo matches.options.enableOnFormTagsAllow firing inside inputs. Defaults to false.options.preventDefaultPrevent default browser action. Defaults to true.voiduseMount
LifecycleRuns a callback exactly once when the component mounts.
(callback: () => void) => voidParameters
callbackFunction to run on mount.voiduseUnmount
LifecycleRuns a callback when the component unmounts. Uses a stable ref to avoid stale closures.
(callback: () => void) => voidParameters
callbackCleanup function to run on unmount.voiduseUpdateEffect
LifecycleIdentical to useEffect but skips the initial run on mount — only fires on subsequent renders.
(effect: EffectCallback, deps?: DependencyList) => voidParameters
effectEffect to run on updates.depsDependency array, same as useEffect.voiduseId
UtilityGenerates a stable unique ID. SSR-safe wrapper around React.useId with optional prefix.
(prefix?: string) => stringStable across re-renders. SSR-safe.
Parameters
prefixOptional string prepended to the ID.string — a stable, unique ID.