use-previous
shadcn-hooks/use-previousFreeHook
A hook to get the previous value of a variable
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-previous.jsonSource
1import { useRef } from 'react'23export type ShouldUpdateFunc<T> = (prev?: T, next?: T) => boolean45const defaultShouldUpdate = <T>(a?: T, b?: T) => !Object.is(a, b)67function usePrevious<T>(8 state: T,9 shouldUpdate: ShouldUpdateFunc<T> = defaultShouldUpdate,10): T | undefined {11 const prevRef = useRef<T>(undefined)12 const curRef = useRef<T>(undefined)1314 if (shouldUpdate(curRef.current, state)) {15 prevRef.current = curRef.current16 curRef.current = state17 }1819 return prevRef.current20}2122export default usePrevious
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
