Skip to main content

Getting Started

@lilib/hooks is a React hook library.

Features

  • Supports React 16.8+, 17+ and 18+.
  • Supports SSR.
  • Supports React StrictMode.
  • Supports network requests.
  • Provides 50+ hooks.

Install

npm install --save @lilib/hooks
# Or
yarn add @lilib/hooks
# Or
pnpm add @lilib/hooks

Example

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

function Example() {
const { data, error, loading } = useLoad(() => {
return fetch("/users/0").then((response) => response.json());
});
...
}

Hooks

CategoryHookDescription
EffectsuseOnceRun effect synchronously only once.
useMountRun effect after the component is mounted.
useUpdateRun effect after the component is updated or the deps are changed, ignore on mount.
useUnmountRun effect on the component unmount.
useTargetEffectIt accepts functions or refs as effect dependencies. It is useful when using DOM nodes as effect dependencies.
useLayoutMountIt's similar to useMount, except it uses useIsomorphicLayoutEffect internally.
useLayoutUpdateIt's similar to useUpdate, except it uses useIsomorphicLayoutEffect internally.
useLayoutTargetEffectIt's similar to useTargetEffect, except it uses useIsomorphicLayoutEffect internally.
useIsomorphicLayoutEffectIn the DOM environment, this hook is the same as React.useLayoutEffect. In the SSR environment, it is the same as React.useEffect.
StatesuseCacheCache data for a certain period of time.
useToggleUse boolean value.
useSetStateThis hook is like this.setState method of class component.
useSafeStateEliminate errors thrown by setting state when the component is unmounted.
useDelayedValueDelay updating a value.
useMemoizedValueReturn the previous value when the value has not changed. Use deep comparison by default.
useThrottledValueThrottle a value.
useDebouncedValueDebounce a value.
CallbacksuseRafInvoke the callback before the browser repainting.
useIdleInvoke the callback when the browser is idle.
useTimeoutDelay running callback.
useIntervalLoop run callback.
useAnimationUse this hook to perform JavaScript animation.
usePersistReturns a function whose identifier never changes.
useRerenderForce rerender the component.
useThrottleUse throttled callback.
useDebounceUse debounced callback.
RefsuseLatestRefReturn a ref object whose .current property is assigned to latest updated value.
usePreviousRefReturn a ref object whose .current property is assigned to previous updated value.
useComposedRefThis hook compose multiple refs into one.
useMountedRefIt returns a ref object that used to determine whether the component is mounted.
useUnmountedRefIt returns a ref object that used to determine whether the component is unmounted.
BrowseruseTitleSet document title.
useFaviconSet document favicon.
useOnlineDetect whether the network is online.
useDarkModeDetect whether the preferred color mode of the system is dark.
usePageVisibleDetect whether the page is visible.
useWindowFocusDetect whether the window is focus.
useMatchMediaDetermine if the document matches the media query string.
useWindowSizeGet window size.
useElementSizeGet DOM element size.
useIntersectingDetermine whether two elements intersect.
useClickOutsideTrigger a callback when clicking outside the target area.
useEventListenerSet up the event listener to the target.
useResizeObserverObserve a element size changing.
useIntersectionObserverObserve whether two elements intersect.
useCookieGet, set and/or remove cookies.
useLocalStorageGet, set and/or remove localStorage.
useSessionStorageGet, set and/or remove sessionStorage.
RequestsuseLoadLoad async data.
useReloadReload async data.
useSubmitSubmit network requests.
ConfigsLoadConfigConfig component of request hooks.
CacheConfigConfig component of useCache.

Changelog

https://github.com/lilibraries/hooks/blob/master/CHANGELOG.md

License

Code licensed MIT, docs CC BY 4.0.