createUseStore
stableSpecialized Hook Factory
Create a bound hook for a specific store instance to avoid passing the store repeatedly.
Signature
createUseStore<T>(store: Store<T>): <U = T>(selector?: (state: T) => U, equalityFn?: (a: U, b: U) => boolean) => U
Parameters
store
Store<T>RequiredThe store instance to bind to the custom hook.
Returns
Returns
FunctionA custom hook that accepts an optional selector and equality function.
Examples
Basic Usage
Create a custom hook for your store and use it in components:
import { createStore, createUseStore } from 'qortex-store-react';// 1. Create your storeconst counterStore = createStore((set, get) => ({count: 0,increment: () => set({ count: get().count + 1 }),}));// 2. Create the specialized hookexport const useCounter = createUseStore(counterStore);// 3. Use it anywherefunction CounterDisplay() {const count = useCounter(s => s.count);return <div>{count}</div>;}
Full Type Safety
The created hook perfectly inherits the types from your store:
interface UserState {name: string;email: string;}const userStore = createStore<UserState>((set) => ({name: 'John',email: 'john@example.com'}));const useUser = createUseStore(userStore);function Profile() {// 's' is automatically typed as UserStateconst name = useUser(s => s.name);return <h1>{name}</h1>;}