Overview
TL;DRโ
- It is a collection of React hooks
- Motivations:
- Make common tasks easier
- Why create these hooks on every project? ๐
Just install this package ๐
- Why choose this package?
- Simple, no over-abstraction
- Only 1.6 kB in size
- Tree-shakeable to remove unused modules from production builds
- TypeScript ๐ฆ
- SSR-friendly ๐
- Unit tested ๐งช
- 557.0BuseCountDown
- 445.0BuseDebounce
- 283.0BuseDebounceFn
- 244.0BuseFirstRender
- 574.0BuseInView
- 498.0BuseIntersection
- 295.0BuseIsomorphicLayoutEffect
- 658.0BuseLocalStorage
- 220.0BusePrevious
- 362.0BusePreviousDistinct
- 563.0BuseSessionStorage
- 477.0BuseThrottle
- 301.0BuseThrottleFn
- 227.0BuseToggle
- 297.0BuseUpdateEffect
- 295.0BuseWindowEvent
Getting Startedโ
- npm
- yarn
- pnpm
npm i react-power-ups
yarn add react-power-ups
pnpm i react-power-ups
Hook Listโ
State ๐ฅโ
useToggle
Easily toggle or set a boolean state.๐ก show/hide modal, show/hide password
useCountDown
Start, pause, resume, and reset a countdown timer.๐ก display a countdown timer, limit certain action within a time frame like resend OTP
useFirstRender
Check if component is on first render (just mounted).๐ก do something only on first render or vice versa
usePrevious
Get the previous state or prop based on the value from previous render.๐ก when you need the value of the previous state/prop
usePreviousDistinct
Get the previous state or prop based on the comparation with current value.๐ก when you need the value of the previous state/prop
useUpdateEffect
Like useEffect hook, but skip on first render.๐ก do something when a state updated
useIsomorphicLayoutEffect
Same as useLayoutEffect, but will not show warning in Next.js (SSR).
Event ๐โ
useIntersection
Tracks an HTML element's intersection.๐ก detect if user has reached the bottom of page to load more data (infinite scroll)
useInView
Tracks an HTML element's visibility on screen or a specified element.๐ก display something differently depending on whether an element is visible on screen or not
useWindowEvent
Add event listener to window.๐ก do something when window received a post-message, user switch tab (window blur event), and many more
Optimization ๐โ
useDebounce
Get debounced value.๐ก limit state update to prevent calling API multiple times
useDebounceFn
Debounces a function.๐ก limit the times a function is called, like limiting API call function when user is typing
useThrottle
Get throttled value.๐ก limit state update to prevent calling API multiple times
useThrottleFn
Throttles a function.๐ก limit the times a function is called, like limiting API call function when user is typing
Storage ๐โ
useLocalStorage
LikeuseState
but persisted onlocalStorage
, SSG/SSR-friendly.๐ก manage theme, language, etc.
useSessionStorage
LikeuseState
but persisted onsessionStorage
, SSG/SSR-friendly.๐ก manage temporary value