React Hooks
@outilx/react-hooks is a high-quality & reliable React Hooks library that provides a collection of commonly used hooks for React applications.
Installation
bash
npm install @outilx/react-hooks
# or
pnpm add @outilx/react-hooks
# or
yarn add @outilx/react-hooksFeatures
- 🎯 State Management - Boolean, toggle, counter, map, and object state hooks
- 💾 Storage - Sync state with localStorage, sessionStorage, cookies, and URL
- ⚡ Performance - Memoization and latest value hooks
- 🔄 Lifecycle - Unmount and effect hooks
- 🚀 Async - Cancelable async task hooks
- 📦 TypeScript - Full TypeScript support with type inference
- ✅ Tested - Comprehensive test coverage
Quick Start
tsx
import { useBoolean, useCounter, useLocalStorageState } from '@outilx/react-hooks';
function App() {
const [visible, { toggle }] = useBoolean(false);
const [count, { inc, dec }] = useCounter(0);
const [user, setUser] = useLocalStorageState('user', { name: 'Guest' });
return (
<div>
<button onClick={toggle}>
{visible ? 'Hide' : 'Show'}
</button>
<div>
<button onClick={() => dec()}>-</button>
<span>{count}</span>
<button onClick={() => inc()}>+</button>
</div>
<p>Welcome, {user.name}</p>
</div>
);
}Categories
State Management Hooks
Manage different types of state with convenient APIs:
useBoolean- Boolean state with toggle, setTrue, setFalseuseToggle- Toggle between two valuesuseCounter- Counter with inc, dec, set, resetuseMap- Map state managementuseSetState- Object state like class component
Storage Hooks
Sync state with various storage mechanisms:
useLocalStorageState- Persist state in localStorageuseSessionStorageState- Persist state in sessionStorageuseCookieState- Sync state with cookiesuseUrlState- Sync state with URL query parameters
Performance Hooks
Optimize your React components:
useLatest- Always get the latest value without re-renderuseMemoizedFn- Memoize function to prevent re-creation
Lifecycle Hooks
Handle component lifecycle:
useUnmount- Execute callback on unmount
Async Hooks
Handle asynchronous operations:
useCancelableAsyncTaskCallback- Create cancelable async tasks
API Reference
For detailed API documentation, see the API Reference.