UNPKG

react-use-anywhere

Version:

Use React hooks anywhere in your codebase - in services, utilities, and business logic. Router-agnostic, hook-agnostic, and works with any React hooks (auth, navigation, state, custom hooks, etc.).

67 lines (65 loc) 2.95 kB
import { HookService, ReactHook, TypedHookService } from '../types'; /** * Creates a service that can store and use hook values from anywhere in your code * * ⚠️ RECOMMENDATION: Use `createSingletonService` instead for better performance and state consistency * * This creates a NEW instance every time it's called. For most React applications, * you want shared state across components, so use `createSingletonService` instead. * * Only use this if you specifically need multiple independent instances. */ export declare function createHookService<T = unknown>(): HookService<T>; /** * 🚀 RECOMMENDED: Create or get a singleton service * * This is the standard way to create services in react-use-anywhere. * Benefits: * - ✅ Shared state across your entire app * - ✅ Better performance (no duplicate instances) * - ✅ Consistent behavior across components * - ✅ Memory efficient * * @param hookName - Name of the hook as registered in HookProvider */ export declare function createSingletonService<T = unknown>(hookName: string): HookService<T>; /** * Get an existing singleton service */ export declare function getSingletonService<T = unknown>(hookName: string): HookService<T> | null; /** * Reset all singleton services (useful for testing) */ export declare function resetAllServices(): void; /** * 🆕 TYPE-SAFE VERSION: Create a service with full type safety * Use this when you want compile-time checking of hook names * * @param hookName - Hook name (will be type-checked against registered hooks) */ export declare function createTypedSingletonService<THooks extends Record<string, ReactHook<unknown>>, K extends keyof THooks>(hookName: K): HookService<ExtractHookType<THooks[K]>>; /** * 🆕 STRICT TYPE-SAFE VERSION: Enforces hook name validation at compile time * This version will show TypeScript errors if you use invalid hook names * * Usage: * ```typescript * // First, define your hooks type * type MyHooks = { * navigate: () => NavigateFunction; * auth: () => AuthState; * }; * * // Then create services with compile-time validation * const navService = createStrictSingletonService<MyHooks>('navigate'); // ✅ Valid * const badService = createStrictSingletonService<MyHooks>('invalid'); // ❌ TypeScript Error * ``` */ export declare function createStrictSingletonService<THooks extends Record<string, ReactHook<unknown>>>(hookName: keyof THooks & string): HookService<ExtractHookType<THooks[typeof hookName]>>; /** * 🆕 INFERRED TYPE-SAFE VERSION: Automatically infers types from provider * This creates services that are automatically typed based on your HookProvider setup */ export declare function createInferredSingletonService<THooks extends Record<string, ReactHook<unknown>>, K extends keyof THooks>(hookName: K): TypedHookService<ExtractHookType<THooks[K]>, THooks>; type ExtractHookType<T> = T extends ReactHook<infer R> ? R : never; export {};