Skip to main content

Getting Started

@lilib/hooks is a collection of practical React hooks covering async data, states, effects, callbacks, refs and browser APIs — written in TypeScript, compatible with React 16.8+, 17, 18 and 19, and safe for SSR and StrictMode.

Features​

  • More than 50 hooks covering async data, effects, states, callbacks, refs and browser APIs.
  • Works with React 16.8+, 17, 18 and 19.
  • SSR-safe — graceful fallbacks outside the browser.
  • StrictMode-safe — idempotent effects with proper cleanup.
  • Fully typed — written in TypeScript.
  • Ships ESM, CJS and UMD builds.
  • Tree-shakeable — bundlers can drop unused hooks from the ESM build.

Install​

npm install @lilib/hooks

Quick start​

import { useLoad } from "@lilib/hooks";

function UserProfile({ userId }: { userId: string }) {
const { data, error, loading } = useLoad(
() => fetch(`/users/${userId}`).then((response) => response.json()),
[userId],
);

if (loading) {
return <div>Loading...</div>;
}

if (error) {
return <div>Something went wrong.</div>;
}

return <div>Hello, {data.name}!</div>;
}

For the full API reference and live examples, check out the docs of each hook listed below.

Hooks​

Async​

HookDescription
useLoadLoad async data, typically for network requests.
useReloadReload all useLoad requests that share the same key.
useSubmitSubmit network requests such as create, update and delete.

Effects​

HookDescription
useMountRun an effect after the component is mounted.
useUpdateRun an effect after the component updates (the initial mount is skipped).
useUnmountRun an effect when the component unmounts.
useRefEffectLike React.useEffect, but ReactRefs in the deps are dereferenced to ref.current before comparison.
useRefUpdateLike useUpdate, but ReactRefs in the deps are dereferenced to ref.current before comparison.
useLayoutMountLike useMount, but runs before the browser paints.
useLayoutUpdateLike useUpdate, but runs before the browser paints.
useLayoutRefEffectLike useRefEffect, but runs before the browser paints.
useLayoutRefUpdateLike useRefUpdate, but runs before the browser paints.
useIsomorphicLayoutEffectReact.useLayoutEffect in the DOM, React.useEffect during SSR.

States​

HookDescription
useCacheCache data for a certain period of time, with optional sync between components sharing the same cache instance.
useToggleManage a boolean value.
useSetStateLike this.setState of class components, merging partial state updates.
useSafeStateLike React.useState, but setting state after unmount is safe.
useDelayedValueDelay updates to a value.
useMemoizedValueReturn the previous value until the value actually changes (deep comparison).
useThrottledValueThrottle a value.
useDebouncedValueDebounce a value.

Callbacks​

HookDescription
useRafInvoke a callback before the browser repaints.
useIdleInvoke a callback when the browser is idle.
useAnimateRun JavaScript animations.
useTimeoutExecute a callback after a delay.
useIntervalRun a callback repeatedly at a fixed interval.
usePersistKeep a stable function identity while always calling the latest logic.
useRerenderRe-render the component manually, usually for mutable data.
useThrottleUse a throttled callback.
useDebounceUse a debounced callback.

Refs​

HookDescription
useLatestRefA ref that always points to the latest value, avoiding stale closures in long-lived callbacks.
usePreviousRefA ref that points to the value from the previous update.
useComposedRefCompose multiple refs (objects or callbacks) into one.
useMountedRefA ref that tells whether the component is mounted.
useUnmountedRefA ref that tells whether the component is unmounted.

Browser​

HookDescription
useTitleSet the document title.
useFaviconSet the document favicon.
useOnlineDetect whether the network is online.
useDarkModeDetect whether the system prefers a dark color scheme.
usePageVisibleDetect whether the page is visible, including bfcache restores.
useWindowFocusDetect whether the window has focus.
useMatchMediaDetermine whether the document matches a media query.
useWindowSizeGet the window size.
useElementSizeGet the size of a DOM element.
useIntersectingDetect whether an element intersects the viewport or a specified root.
useClickOutsideTrigger a callback when clicking outside the target area.
useEventListenerSet up an event listener on the target.
useResizeObserverObserve an element's size changes.
useIntersectionObserverObserve changes in an element's intersection with another element or the viewport.
useCookieGet, set and/or remove cookies.
useLocalStorageGet, set and/or remove localStorage.
useSessionStorageGet, set and/or remove sessionStorage.

Changelog​

See CHANGELOG.md for the full release history.

License​

MIT