Skip to content

Commit

Permalink
feat: add buildSelector arguments props
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatoVan committed Apr 6, 2024
1 parent 33f6f39 commit 958b794
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/shared/lib/store/buildSelector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { useSelector } from 'react-redux';
import { StateSchema } from '@/app/providers/StoreProvider';

type Selector<T> = (state: StateSchema) => T;
type Result<T> = [() => T, Selector<T>];
type Selector<T, Args extends any[]> = (state: StateSchema, ...args: Args) => T;
type Hook<T, Args extends any[]> = (...args: Args) => T;
type Result<T, Args extends any[]> = [Hook<T, Args>, Selector<T, Args>];

export function buildSelector<T>(selector: Selector<T>): Result<T> {
const useSelectorHook = () => useSelector(selector);
export function buildSelector<T, Args extends any[]>(
selector: Selector<T, Args>,
): Result<T, Args> {
const useSelectorHook: Hook<T, Args> = (...args: Args) =>
useSelector((state: StateSchema) => selector(state, ...args));

return [useSelectorHook, selector];
}

0 comments on commit 958b794

Please sign in to comment.