-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom useSelector and dispatch
- Loading branch information
Showing
6 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { StateSchema } from '@/app/providers/StoreProvider'; | ||
|
||
type Selector<T> = (state: StateSchema) => T; | ||
type Result<T> = [() => T, Selector<T>] | ||
|
||
export function buildSelector<T>(selector: Selector<T>): Result<T> { | ||
const useSelectorHook = () => useSelector(selector); | ||
|
||
return [useSelectorHook, selector]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { bindActionCreators, createSlice } from '@reduxjs/toolkit'; | ||
import { SliceCaseReducers, CreateSliceOptions } from '@reduxjs/toolkit/dist'; | ||
import { useDispatch } from 'react-redux'; | ||
import { useMemo } from 'react'; | ||
|
||
export function buildSlice< | ||
State, | ||
CaseReducers extends SliceCaseReducers<State>, | ||
Name extends string = string | ||
>(options: CreateSliceOptions<State, CaseReducers, Name>) { | ||
const slice = createSlice(options); | ||
|
||
const useActions = (): typeof slice.actions => { | ||
const dispatch = useDispatch(); | ||
|
||
// @ts-ignore | ||
return useMemo(() => bindActionCreators(slice.actions, dispatch), [dispatch]); | ||
}; | ||
|
||
return { | ||
...slice, | ||
useActions, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { buildSelector } from './buildSelector'; | ||
export { buildSlice } from './buildSlice'; |