react-hooks-bank
Version:
A collection of **powerful, reusable custom React hooks** for complex, non-trivial interactions that go beyond Reactβs native features.
353 lines (212 loc) β’ 6.5 kB
Markdown
# π react-hooks-bank
A collection of **powerful, reusable custom React hooks** for complex, non-trivial interactions that go beyond Reactβs native features.
Built with performance and developer ergonomics in mind, these hooks help you accelerate development and build more interactive experiences effortlessly.
## π Features
* π Fully typed with **TypeScript**
* π§ Designed for **advanced and non-standard use cases**
* π§Ή Pluggable and composable React hooks
* π§ Minimal dependencies, lightweight and tree-shakable
* π Open-source and community-driven
## π Installation
```bash
npm install react-hooks-bank
```
Or using `yarn`:
```bash
yarn add react-hooks-bank
```
## π§Ή Usage Example (General)
Import the hooks you need and use them in your components:
```tsx
import { useInfiniteScroll } from "react-hooks-bank";
const MyComponent = () => {
const isLoading = useInfiniteScroll(() => {
// Callback when scroll reaches threshold
fetchMoreItems();
});
return (
<div>
{/* Your list rendering */}
{isLoading && <p>Loading more...</p>}
</div>
);
};
```
## πΉ Available Hooks
Click a hook to jump to detailed usage instructions:
* [`useClickOutside`](#useclickoutside)
* [`useDebouncedValue`](#usedebouncedvalue)
* [`useFetch`](#usefetch)
* [`useGeolocation`](#usegeolocation)
* [`useHover`](#usehover)
* [`useInfiniteScroll`](#useinfinitescroll)
* [`useIntersectionObserver`](#useintersectionobserver)
* [`useLockBodyScroll`](#uselockbodyscroll)
* [`useNetworkStatus`](#usenetworkstatus)
* [`useThrottle`](#usethrottle)
* [`useDeviceInfo`](#usedeviceinfo)
* [`useOrientation`](#useorientation)
* [`useCopyToClipboard`](#usecopytoclipboard)
## π§ How to Use Each Hook
### `useClickOutside`
**Purpose**: Detect clicks outside a specific DOM element.
**How to use**:
```tsx
const ref = useRef(null);
useClickOutside(ref, () => console.log("Clicked outside!"));
```
**Returns**: `void`
**Parameters**:
* `ref: RefObject<HTMLElement>` - **Required**: Element to monitor
* `callback: () => void` - **Required**: Triggered when clicking outside
### `useDebouncedValue`
**Purpose**: Return a debounced version of a value.
**How to use**:
```tsx
const debouncedValue = useDebouncedValue(searchTerm, 500);
```
**Returns**: `any` β Debounced value
**Parameters**:
* `value: any` β **Required**: The value to debounce
* `delay: number` β **Required**: Delay in ms
### `useFetch`
**Purpose**: Fetch data with loading and error state.
**How to use**:
```tsx
const { data, loading, error } = useFetch('/api/data');
```
**Returns**: `{ data, loading, error }`
**Parameters**:
* `url: string` β **Required**: Endpoint URL
* `options?: RequestInit` β Optional fetch options
### `useGeolocation`
**Purpose**: Track user's geolocation.
**How to use**:
```tsx
const { coords, error } = useGeolocation();
```
**Returns**: `{ coords, error }`
**Parameters**:
* `options?: PositionOptions` β Optional navigator geolocation options
### `useHover`
**Purpose**: Detect hover state on an element.
**How to use**:
```tsx
const { ref, hovered } = useHover();
```
**Returns**: `{ ref, hovered }`
**Parameters**: None
### `useInfiniteScroll`
**Purpose**: Trigger callback when scrolled to bottom threshold.
**How to use**:
```tsx
const isLoading = useInfiniteScroll(() => loadMoreItems());
```
**Returns**: `boolean` β `true` if loading
**Parameters**:
* `callback: () => void` β **Required**
* `threshold?: number` β Distance in px to bottom (default: 100)
### `useIntersectionObserver`
**Purpose**: Observe if an element is visible in viewport.
**How to use**:
```tsx
const { ref, entry } = useIntersectionObserver();
```
**Returns**: `{ ref, entry }`
**Parameters**:
* `options?: IntersectionObserverInit`
### `useLockBodyScroll`
**Purpose**: Prevent body scroll when active.
**How to use**:
```tsx
useLockBodyScroll();
```
**Returns**: `void`
**Parameters**: None
### `useNetworkStatus`
**Purpose**: Detect online/offline network status.
**How to use**:
```tsx
const isOnline = useNetworkStatus();
```
**Returns**: `boolean`
**Parameters**: None
### `useThrottle`
**Purpose**: Throttle a value change over time.
**How to use**:
```tsx
const throttled = useThrottle(value, 300);
```
**Returns**: `any` β Throttled value
**Parameters**:
* `value: any` β **Required**
* `delay: number` β **Required**
### `useDeviceInfo`
**Purpose**: Retrieve device OS, browser, and screen info.
**How to use**:
```tsx
const { os, browser, screen } = useDeviceInfo();
```
**Returns**: `{ os, browser, screen }`
**Parameters**: None
### `useOrientation`
**Purpose**: Track screen orientation and angle.
**How to use**:
```tsx
const { angle, type } = useOrientation();
```
**Returns**: `{ angle: number, type: string }`
**Parameters**: None
### `useCopyToClipboard`
**Purpose**: Simplifies copying text to the userβs clipboard and reporting success/failure.
**How to use**:
```tsx
const [ copy, isCopied ] = useCopyToClipboard();
```
**Returns**: `[ copy: (text: string) => Promise<boolean>, isCopied: boolean ]`
**Parameters**: None
## π Local Development
To run the project locally:
```bash
git clone https://github.com/mysticwillz/react-hooks-bank.git
cd react-hooks-bank
npm install
npm run build
```
## π€ Contributing
Contributions are very welcome!
If you'd like to add a new hook, improve documentation, or fix a bug:
1. Fork the repo
2. Create a new branch
3. Submit a PR
Please follow the guidelines in [`CONTRIBUTING.md`](./CONTRIBUTING.md).
## π License
This project is licensed under the MIT License
## π€ Author
Built with β€οΈ by [Eze Williams Ezebuilo](https://github.com/mysticwillz)
* Twitter: [@mysticwillz](https://twitter.com/mysticwillz)
* LinkedIn: [mysticwillz](https://linkedin.com/in/mysticwillz)
## β Support & Collaboration
If this project helps you or youβd like to collaborate, star the repo and share it with other developers!