react-use-anywhere
Use React Hooks Anywhere in Your Codebase
Enable React hooks in services, utilities, and business logic - not just components. Break free from component boundaries without breaking React rules.
Quick Install
npm install react-use-anywhereWhy react-use-anywhere?
React hooks are powerful, but they`re trapped in components. Until now.
The Problem
Hooks Only Work in Components
Forces Prop Drilling
Pass navigation, auth, and other hook values through multiple component layers just to use them in utilities
Scattered Business Logic
Logic that belongs in services gets trapped in components, making testing and reuse harder
The Solution
Use Hooks Anywhere
Clean Architecture
Keep business logic in services where it belongs. No prop drilling needed.
Better Testing
Mock services easily without mounting components or complex setup
Perfect for Real-World Apps
Handle 401s by redirecting to login from your fetch wrapper
Business logic can access router, auth, and other hooks
Helper functions can show notifications or navigate
How It Works
Simple steps to use hooks anywhere
Configure Logging
OptionalEnable debug logging during development (disabled by default for clean production logs)
// In main.tsx or index.tsx (before rendering app)
import { configureLogging } from 'react-use-anywhere';
// Enable only in development
if (import.meta.env.DEV) {
configureLogging({ enabled: true });
}
// Or use environment variable
configureLogging({
enabled: import.meta.env.VITE_DEBUG === 'true'
});💡 When enabled, you'll see hook connections, updates, and errors in console
Wrap Your App
Add HookProvider to your app root and register your hooks
import { HookProvider } from 'react-use-anywhere';
import { useNavigate } from 'react-router-dom';
function App() {
return (
<HookProvider hooks={{
navigate: useNavigate,
auth: useAuth,
theme: useTheme
}}>
<YourApp />
</HookProvider>
);
}Create Services
Create singleton services for each hook you want to use
import { createSingletonService } from 'react-use-anywhere';
// Create one service per hook
export const authService = createSingletonService('auth');
export const navService = createSingletonService('navigate');
export const themeService = createSingletonService('theme');Connect in Component
Call useHookService in at least one component to connect the service
import { useHookService } from 'react-use-anywhere';
import { authService, navService } from './services';
function MyComponent() {
// REQUIRED: Connect services to hooks
useHookService(authService, 'auth');
useHookService(navService, 'navigate');
return <YourContent />;
}Use Anywhere!
Now use your services in utilities, API clients, business logic - anywhere!
// In services/auth.ts, utils/api.ts, anywhere!
import { authService, navService } from './services';
export const logout = () => {
authService.use((auth) => auth.logout());
navService.use((nav) => nav('/login'));
};
export async function fetchData(url: string) {
const response = await fetch(url);
if (response.status === 401) {
authService.use((auth) => auth.logout());
navService.use((nav) => nav('/login'));
}
return response.json();
}That`s it! 🎉
Your hooks are now available anywhere in your codebase
Core Features
Built for production apps
Singleton Pattern
Each hook gets a single service instance shared across your entire app
- ▸Automatic service registration and management
- ▸No manual wiring or dependency injection needed
- ▸One source of truth for each hook value
Type Safety
Full TypeScript support with strict typing options
- ▸Infer types automatically from your hooks
- ▸Compile-time validation with createStrictSingletonService
- ▸Complete IntelliSense support in services
Testing Made Easy
Mock services without complex component mounting
- ▸Simple _setValue() for test mocks
- ▸resetAllServices() for clean test isolation
- ▸Test business logic independently
Universal Compatibility
Works with any React router and custom hooks
- ▸React Router, TanStack, Next.js, Remix support
- ▸Compatible with any custom hook
- ▸Works with React 16.8+ through 19.x
Optional Logging
Debug-friendly logging that's disabled by default
- ▸Enable/disable logging with configureLogging()
- ▸Logs only appear in development mode
- ▸Clean console in production by default
Service API Methods
Execute callback with hook value. Returns result or null if not ready.
Get current value (null if not ready).
Check if service is connected and ready.
Real-World Use Cases
See how react-use-anywhere solves common problems
API Authentication
Handle auth errors in your API client
export async function fetchAPI(url: string) {
const response = await fetch(url);
if (response.status === 401) {
// Logout and redirect from API client!
authService.use(auth => auth.logout());
navService.use(nav => nav('/login'));
throw new Error('Unauthorized');
}
return response.json();
}Navigation Utilities
Navigate from anywhere, not just components
export function navigateToProfile(userId: string) {
navService.use(nav => nav(`/profile/${userId}`));
}
export function goBack() {
navService.use(nav => nav(-1));
}
// Use in event handlers, callbacks, timers
setTimeout(() => navigateToProfile('123'), 2000);Business Logic
Complex workflows with multiple hook interactions
export async function checkout() {
const items = cartService.use(c => c.items) || [];
if (items.length === 0) {
notifyService.use(n => n.warning('Cart empty'));
return;
}
try {
await processPayment(items);
cartService.use(c => c.clear());
notifyService.use(n => n.success('Order placed!'));
navService.use(nav => nav('/confirmation'));
} catch (error) {
notifyService.use(n => n.error('Payment failed'));
}
}Event Handlers
WebSocket, timers, and external event responses
// WebSocket message handler
socket.on('notification', (data) => {
notifyService.use(notify =>
notify.info(data.message)
);
});
// Timer-based actions
setInterval(() => {
const isAuth = authService.use(a => a.isAuthenticated);
if (!isAuth) {
navService.use(nav => nav('/login'));
}
}, 60000);Common Patterns
Auth + Navigation
Combine multiple services for coordinated actions
Conditional Logic
Check hook values and take different actions
Error Handling
Handle errors with notifications and redirects
Multi-Step Workflows
Chain multiple operations across services
Get Started
Install and start using hooks anywhere in minutes
Installation
npm
yarn
pnpm
Quick Start
0. Optional: Configure Logging
Enable logging for debugging (disabled by default)
// In main.tsx or index.tsx (before app render)
import { configureLogging } from 'react-use-anywhere';
// Enable logging for development
if (import.meta.env.DEV) {
configureLogging({ enabled: true });
}1. Setup Provider
import { HookProvider } from 'react-use-anywhere';
import { useNavigate } from 'react-router-dom';
function App() {
return (
<HookProvider hooks={{ navigate: useNavigate }}>
<YourApp />
</HookProvider>
);
}2. Create Service
import { createSingletonService } from 'react-use-anywhere';
export const navService = createSingletonService('navigate');3. Connect in Component
import { useHookService } from 'react-use-anywhere';
function MyComponent() {
useHookService(navService, 'navigate');
return <YourContent />;
}4. Use Anywhere!
// In any file: services, utils, API clients
export const goToHome = () => {
navService.use(nav => nav('/'));
};Key Points to Remember
Requirements
Zero dependencies • <2KB gzipped