
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
modern-react-hooks
Advanced tools
Zero-dependency, TypeScript-first React hooks for modern development
Zero-dependency, TypeScript-first React hooks for modern development
npm install modern-react-hooks
yarn add modern-react-hooks
pnpm add modern-react-hooks
import { useLocalStorage, useDebounce } from 'modern-react-hooks'
function App() {
const [theme, setTheme] = useLocalStorage('theme', 'light')
const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebounce(searchTerm, 300)
return (
<div>
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle theme: {theme}
</button>
<input
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search..."
/>
<p>Debounced: {debouncedSearchTerm}</p>
</div>
)
}
useLocalStorage<T>(key, initialValue, options?)Persistent state with localStorage synchronization.
const [user, setUser] = useLocalStorage('user', { name: 'Guest' })
const [count, setCount, removeCount] = useLocalStorage('count', 0)
// With options
const [settings, setSettings] = useLocalStorage('settings', {}, {
syncAcrossTabs: true,
onError: (error) => console.warn(error),
serializer: customSerializer
})
Parameters:
key: string - localStorage keyinitialValue: T - Default value when key doesn't existoptions?: StorageOptions<T> - Configuration optionsReturns: [value, setValue, removeValue]
useToggle<T>(initialValue, toggleFunction?)Boolean toggle state with custom toggle logic.
const [isOpen, toggle] = useToggle(false)
const [status, toggleStatus] = useToggle('active', (current) =>
current === 'active' ? 'inactive' : 'active'
)
toggle() // Toggles between true/false
toggle(true) // Sets to specific value
Parameters:
initialValue: T - Initial state valuetoggleFunction?: (value: T) => T - Custom toggle logicReturns: [value, toggle]
useDebounce<T>(value, delay)Debounce rapidly changing values.
const [searchQuery, setSearchQuery] = useState('')
const debouncedQuery = useDebounce(searchQuery, 500)
useEffect(() => {
if (debouncedQuery) {
searchAPI(debouncedQuery)
}
}, [debouncedQuery])
Parameters:
value: T - Value to debouncedelay: number - Delay in millisecondsReturns: T - Debounced value
useClickOutside<T>(handler, options?)Detect clicks outside an element.
const [isOpen, setIsOpen] = useState(false)
const modalRef = useClickOutside<HTMLDivElement>(() => {
setIsOpen(false)
})
return (
<div ref={modalRef} className="modal">
Modal content
</div>
)
Parameters:
handler: (event: Event) => void - Callback for outside clicksoptions?: UseClickOutsideOptions - Configuration optionsReturns: RefObject<T> - Ref to attach to element
Import only what you need for optimal bundle size:
// Import specific hooks (recommended)
import { useLocalStorage } from 'modern-react-hooks/useLocalStorage'
import { useDebounce } from 'modern-react-hooks/useDebounce'
// Or import from main entry
import { useLocalStorage, useDebounce } from 'modern-react-hooks'
| Hook | Gzipped Size |
|---|---|
useLocalStorage | ~800 bytes |
useDebounce | ~400 bytes |
useClickOutside | ~600 bytes |
useToggle | ~200 bytes |
| Total Bundle | ~15kb |
Compare with alternatives:
Perfect TypeScript integration with full type inference:
// Type is automatically inferred as string
const [name, setName] = useLocalStorage('username', 'guest')
// Explicit typing for complex objects
interface User {
id: number
name: string
email: string
}
const [user, setUser] = useLocalStorage<User>('user', {
id: 0,
name: '',
email: ''
})
// Custom serializer with proper typing
const dateSerializer = {
parse: (value: string): Date => new Date(value),
stringify: (value: Date): string => value.toISOString()
}
const [lastLogin, setLastLogin] = useLocalStorage('lastLogin', new Date(), {
serializer: dateSerializer
})
All hooks are SSR-safe and work with:
// Safe to use in SSR environments
function MyComponent() {
const [theme, setTheme] = useLocalStorage('theme', 'light')
// Initial render will use 'light', then hydrate with actual value
return <div className={theme}>Content</div>
}
Built-in error handling with optional custom error callbacks:
const [data, setData] = useLocalStorage('data', null, {
onError: (error) => {
// Custom error handling
console.error('Storage error:', error)
analytics.track('storage_error', { error: error.message })
}
})
# Install dependencies
npm install
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Build the library
npm run build
# Run type checking
npm run type-check
# Lint code
npm run lint
We welcome contributions! Please see our Contributing Guide for details.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)MIT © kynuxdev
useCounter - Numeric counter with min/max boundsuseBoolean - Boolean state utilitiesuseArray - Array state managementuseCopyToClipboard - Clipboard operationsuseThrottle - Throttled functionsusePrevious - Previous value trackinguseSessionStorage - Session storage stateMade with ❤️ for the React community
FAQs
Zero-dependency, TypeScript-first React hooks for modern development
We found that modern-react-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.