A collection of reusable custom React hooks for efficient development
# npm
npm install react-hook-granth
# yarn
yarn add react-hook-granth
# pnpm
pnpm add react-hook-granth
- πͺ Custom React Hooks β Counter, debounce, storage, event listeners, and more
- β‘ Lightweight β Zero dependencies, tree-shakable
- π¦ TypeScript Support β Full type definitions included
- π§ͺ Well Tested β Comprehensive test coverage
- π Well Documented β Clear examples and API documentation
import { useCounter, useLocalStorage, useDebounce } from 'react-hook-granth';
function App() {
const { count, increment, decrement, reset } = useCounter(0);
const [name, setName] = useLocalStorage('username', '');
const debouncedName = useDebounce(name, 300);
return (
<div>
<div>
<p>Count: {count}</p>
<button onClick={increment}>β Increment</button>
<button onClick={decrement}>β Decrement</button>
<button onClick={reset}>π Reset</button>
</div>
<div>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
/>
<p>Debounced: {debouncedName}</p>
</div>
</div>
);
}
---
## π Available Hooks
### π’ State Management
| Hook | Description | Example |
| ----------------- | ----------------------------------- | -------------------------------- |
| `useCounter` | Increment, decrement, reset counter | [View Example](#usecounter) |
| `useLocalStorage` | Persist state in localStorage | [View Example](#uselocalstorage) |
| `usePrevious` | Track previous value of state/prop | [View Example](#useprevious) |
### β‘ Performance & Effects
| Hook | Description | Example |
| ------------------- | -------------------------------- | ---------------------------------- |
| `useDebounce` | Debounce rapidly changing values | [View Example](#usedebounce) |
| `useScrollIntoView` | Auto-scroll element into view | [View Example](#usescrollintoview) |
### π±οΈ User Interactions
| Hook | Description | Example |
| -------------------- | ----------------------------- | ----------------------------------- |
| `useClickOutside` | Detect clicks outside element | [View Example](#useclickoutside) |
| `useCopyToClipboard` | Copy text to clipboard | [View Example](#usecopytoclipboard) |
| `useEventListener` | Attach event listeners safely | Coming Soon |
### π Browser APIs
| Hook | Description | Example |
| --------------- | ----------------------- | ----------- |
| `useWindowSize` | Track window dimensions | Coming Soon |
---
## π Hook Examples
### useCounter
```jsx
import { useCounter } from 'react-hook-granth';
function CounterExample() {
const { count, increment, decrement, reset } = useCounter(10);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<button onClick={reset}>Reset to 10</button>
</div>
);
}import { useState } from 'react';
import { useDebounce } from 'react-hook-granth';
function SearchExample() {
const [searchTerm, setSearchTerm] = useState('');
const debouncedValue = useDebounce(searchTerm, 300);
return (
<div>
<input
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search..."
/>
<p>Debounced: {debouncedValue}</p>
</div>
);
}import { useLocalStorage } from 'react-hook-granth';
function SettingsExample() {
const [theme, setTheme] = useLocalStorage('theme', 'light');
return (
<div>
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Current theme: {theme}
</button>
</div>
);
}# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch- TypeScript Support - Full type definitions
- Tree Shaking - Import only what you need
- SSR Compatible - Works with Next.js, Gatsby, etc.
- Zero Dependencies - No external runtime dependencies
- Comprehensive Tests - 100% test coverage
- Multiple Formats - ESM and CommonJS builds
# Build for production
npm run build
# Clean build artifacts
npm run clean- Total Bundle: ~4.2KB minified + gzipped
- Individual Hooks: 0.5KB - 1.2KB each
- Tree-shakable: Import only what you use
All hooks are SSR-safe and work with:
- Next.js
- Gatsby
- Remix
- Any SSR framework
We welcome contributions from the community! Whether you're fixing bugs, adding new hooks, or improving documentation, your help is appreciated.
- Fork the repository on GitHub
- Clone your fork locally:
git clone https://github.com/YOUR_USERNAME/react-hook-granth.git cd react-hook-granth - Install dependencies:
npm install
- Create a new branch for your feature:
git checkout -b feature/your-hook-name
-
Create the hook in
src/hooks/:// src/hooks/useYourHook.ts import { useState, useEffect } from 'react'; export function useYourHook(initialValue: any) { // Your hook implementation return { /* your return values */ }; }
-
Export it from
src/index.ts:export { useYourHook } from './hooks/useYourHook';
-
Add TypeScript types (if needed) in
src/types/ -
Write tests (optional but encouraged) in
src/__tests__/:// src/__tests__/useYourHook.test.ts import { renderHook } from '@testing-library/react'; import { useYourHook } from '../hooks/useYourHook'; describe('useYourHook', () => { it('should work correctly', () => { const { result } = renderHook(() => useYourHook('test')); expect(result.current).toBeDefined(); }); });
-
Update documentation by adding your hook to the README.md
- TypeScript: All hooks should be written in TypeScript with proper type definitions
- ESLint: Follow the existing ESLint configuration
- Naming: Use descriptive names starting with
use(e.g.,useLocalStorage,useDebounce) - Documentation: Include JSDoc comments for better IntelliSense
While tests are not mandatory, they help ensure code quality and prevent regressions:
- Use Jest and React Testing Library
- Test edge cases and error scenarios
- Aim for good coverage of your hook's functionality
- Run tests with:
npm test
- Ensure your code follows the project's coding standards
- Update the README.md with details of your new hook (if applicable)
- Test your changes thoroughly
- Commit your changes with clear, descriptive messages:
git commit -m "feat: add useYourHook for handling X functionality" - Push to your fork:
git push origin feature/your-hook-name
- Create a Pull Request with:
- Clear title and description
- Reference any related issues
- Screenshots or examples (if applicable)
Looking for ways to contribute? Here are some ideas:
useMediaQuery- Responsive design helperuseIntersectionObserver- Visibility detectionuseFetch- Data fetching with cachinguseForm- Form state managementuseKeyboard- Keyboard shortcutsuseGeolocation- Browser geolocation APIuseOnlineStatus- Network status detectionuseIdle- User idle detection
- Bug fixes for existing hooks
- Performance optimizations
- Better TypeScript types
- Documentation improvements
- Example applications
- Test coverage for existing hooks
- Issues: Open an issue for bugs or feature requests
- Discussions: Use GitHub Discussions for questions
- Code Review: Maintainers will review your PR and provide feedback
All contributors will be:
- Added to the contributors list
- Mentioned in release notes
- Given credit in the documentation
Thank you for helping make React Hook Granth better! π
Want to see what's coming next? Check out our GitHub Issues for planned features and improvements.
Vote on existing issues or create new ones to help us understand what the community needs most!
MIT Β© Yuvraj Karna
If this library helps you, please consider:
- βοΈ Star the repository
- π Report issues or suggest improvements
- π‘ Contribute new hooks or improvements
- π’ Share it with other developers
Built with β€οΈ for the React community