npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

modern-react-hooks

v1.0.1

Published

Zero-dependency, TypeScript-first React hooks for modern development

Readme

modern-react-hooks

Zero-dependency, TypeScript-first React hooks for modern development

npm version License: MIT TypeScript

Why modern-react-hooks?

  • 🚀 2-3x smaller than alternatives (15kb vs 45kb gzipped)
  • 💪 100% TypeScript with perfect inference
  • 🔒 Zero dependencies for maximum security
  • 🌳 Tree-shakeable - import only what you need
  • React 18+ optimized with Concurrent features
  • 🧪 100% test coverage for reliability
  • 📦 ESM + CJS dual build support
  • 🎯 Modern JavaScript (ES2020+)

Installation

npm install modern-react-hooks
yarn add modern-react-hooks
pnpm add modern-react-hooks

Quick Start

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>
  )
}

Available Hooks

State Management

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 key
  • initialValue: T - Default value when key doesn't exist
  • options?: StorageOptions<T> - Configuration options

Returns: [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 value
  • toggleFunction?: (value: T) => T - Custom toggle logic

Returns: [value, toggle]

Performance Optimization

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 debounce
  • delay: number - Delay in milliseconds

Returns: T - Debounced value

DOM & Events

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 clicks
  • options?: UseClickOutsideOptions - Configuration options

Returns: RefObject<T> - Ref to attach to element

Tree Shaking & Bundle Size

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'

Bundle Size Comparison

| Hook | Gzipped Size | |------|--------------| | useLocalStorage | ~800 bytes | | useDebounce | ~400 bytes | | useClickOutside | ~600 bytes | | useToggle | ~200 bytes | | Total Bundle | ~15kb |

Compare with alternatives:

  • react-use: ~45kb gzipped
  • rooks: ~35kb gzipped
  • ahooks: ~50kb gzipped

TypeScript Support

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
})

Server-Side Rendering (SSR)

All hooks are SSR-safe and work with:

  • Next.js
  • Gatsby
  • Remix
  • Any SSR React framework
// 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>
}

Error Handling

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 })
  }
})

Browser Support

  • Chrome 90+
  • Firefox 88+
  • Safari 14+
  • Edge 90+

Development

# 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

Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT © kynuxdev

Roadmap

  • [ ] useCounter - Numeric counter with min/max bounds
  • [ ] useBoolean - Boolean state utilities
  • [ ] useArray - Array state management
  • [ ] useCopyToClipboard - Clipboard operations
  • [ ] useThrottle - Throttled functions
  • [ ] usePrevious - Previous value tracking
  • [ ] useSessionStorage - Session storage state
  • [ ] Performance optimizations
  • [ ] React 19 compatibility
  • [ ] Storybook documentation

Made with ❤️ for the React community