Hooks
A collection of reusable React hooks for common UI patterns and state management.
Installation
pnpm add @akinon/hooksuseDebounce
Debounces a value that changes over time. Delays updating the value until after a specified wait time has elapsed since the last change.
Usage
import { useDebounce } from '@akinon/hooks';
const SearchInput = () => {
const [searchTerm, setSearchTerm] = useState('');
const debouncedSearchTerm = useDebounce(searchTerm, 500);
useEffect(() => {
if (debouncedSearchTerm) {
// API call only fires 500ms after user stops typing
searchProducts(debouncedSearchTerm);
}
}, [debouncedSearchTerm]);
return (
<Input
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search products..."
/>
);
};Parameters
value
T
The value to debounce
delay
number
Delay in milliseconds
Returns
T
The debounced value
useThrottle
Throttles a value that changes over time. Limits how frequently the value can update.
Usage
Parameters
value
T
The value to throttle
delay
number
Throttle interval in milliseconds
Returns
T
The throttled value
useLocalStorage
Persists state to localStorage with automatic serialization/deserialization.
Usage
With Objects
Raw Mode
Parameters
key
string
localStorage key
initialValue
T
Initial value if key doesn't exist
options
ParserOptions
Serialization options
Returns
0
T | undefined
Current value
1
Dispatch<SetStateAction<T>>
Setter function
2
() => void
Remove function
useSessionStorage
Persists state to sessionStorage. Similar to useLocalStorage but data is cleared when the browser session ends.
Usage
Parameters
key
string
sessionStorage key
initialValue
T
Initial value if key doesn't exist
raw
boolean
Store as raw string (default: false)
Returns
0
T
Current value
1
(value: T) => void
Setter function
useToggle
Toggles a boolean state. Useful for UI states like open/close, show/hide, enabled/disabled.
Usage
Multiple Toggles
Parameters
initialValue
boolean
false
Initial toggle state
Returns
0
boolean
Current value
1
() => void
Toggle function
useMount
Runs a function when the component mounts. A cleaner alternative to useEffect with empty dependency array.
Usage
Parameters
fn
() => void
Function to run on mount
usePrevious
Returns the previous value of a state or prop. Useful for comparing values between renders.
Usage
Detecting Changes
Parameters
value
T
Value to track
Returns
T | undefined
Previous value (undefined on first render)
useWindowSize
Returns the current window dimensions and updates on resize.
Usage
Responsive Grid
Returns
width
number
Window inner width
height
number
Window inner height
Best Practices
Use debounce for input - Debounce search inputs and form fields to reduce API calls
Use throttle for scroll/resize - Throttle scroll and resize handlers for better performance
Prefer useLocalStorage for persistence - Use for user preferences that should persist
Use useSessionStorage for temporary data - Wizard forms, multi-step processes
Use useToggle for boolean states - Cleaner than managing boolean state manually
Last updated
Was this helpful?

