Hooks

A collection of reusable React hooks for common UI patterns and state management.

Installation

pnpm add @akinon/hooks

useDebounce

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

Parameter
Type
Description

value

T

The value to debounce

delay

number

Delay in milliseconds

Returns

Type
Description

T

The debounced value


useThrottle

Throttles a value that changes over time. Limits how frequently the value can update.

Usage

Parameters

Parameter
Type
Description

value

T

The value to throttle

delay

number

Throttle interval in milliseconds

Returns

Type
Description

T

The throttled value


useLocalStorage

Persists state to localStorage with automatic serialization/deserialization.

Usage

With Objects

Raw Mode

Parameters

Parameter
Type
Description

key

string

localStorage key

initialValue

T

Initial value if key doesn't exist

options

ParserOptions

Serialization options

Returns

Index
Type
Description

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

Parameter
Type
Description

key

string

sessionStorage key

initialValue

T

Initial value if key doesn't exist

raw

boolean

Store as raw string (default: false)

Returns

Index
Type
Description

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

Parameter
Type
Default
Description

initialValue

boolean

false

Initial toggle state

Returns

Index
Type
Description

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

Parameter
Type
Description

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

Parameter
Type
Description

value

T

Value to track

Returns

Type
Description

T | undefined

Previous value (undefined on first render)


useWindowSize

Returns the current window dimensions and updates on resize.

Usage

Responsive Grid

Returns

Property
Type
Description

width

number

Window inner width

height

number

Window inner height


Best Practices

  1. Use debounce for input - Debounce search inputs and form fields to reduce API calls

  2. Use throttle for scroll/resize - Throttle scroll and resize handlers for better performance

  3. Prefer useLocalStorage for persistence - Use for user preferences that should persist

  4. Use useSessionStorage for temporary data - Wizard forms, multi-step processes

  5. Use useToggle for boolean states - Cleaner than managing boolean state manually

Last updated

Was this helpful?