createUseStore

stable

Specialized 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>Required

The store instance to bind to the custom hook.

Returns

Returns

Function

A 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 store
const counterStore = createStore((set, get) => ({
count: 0,
increment: () => set({ count: get().count + 1 }),
}));
// 2. Create the specialized hook
export const useCounter = createUseStore(counterStore);
// 3. Use it anywhere
function 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 UserState
const name = useUser(s => s.name);
return <h1>{name}</h1>;
}

Related Pages