-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Albert-Gao/v2
BREAKING CHANGE: updated the support for zustand v4
- Loading branch information
Showing
9 changed files
with
2,116 additions
and
1,642 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { StoreApi, UseBoundStore } from 'zustand'; | ||
|
||
export interface ZustandFuncSelectors<StateType> { | ||
use: { | ||
[key in keyof StateType]: () => StateType[key]; | ||
}; | ||
} | ||
|
||
export function createSelectorFunctions<StateType extends object>( | ||
store: UseBoundStore<StoreApi<StateType>> | ||
) { | ||
const storeIn = store as any; | ||
|
||
storeIn.use = {}; | ||
|
||
Object.keys(storeIn.getState()).forEach((key) => { | ||
const selector = (state: StateType) => state[key as keyof StateType]; | ||
storeIn.use[key] = () => storeIn(selector); | ||
}); | ||
|
||
return store as UseBoundStore<StoreApi<StateType>> & | ||
ZustandFuncSelectors<StateType>; | ||
} |
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,23 @@ | ||
import { StoreApi, UseBoundStore } from 'zustand'; | ||
|
||
export type ZustandHookSelectors<StateType> = { | ||
[Key in keyof StateType as `use${Capitalize< | ||
string & Key | ||
>}`]: () => StateType[Key]; | ||
}; | ||
|
||
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); | ||
|
||
export function createSelectorHooks<StateType extends object>( | ||
store: UseBoundStore<StoreApi<StateType>> | ||
) { | ||
const storeIn = store as any; | ||
|
||
Object.keys(storeIn.getState()).forEach((key) => { | ||
const selector = (state: StateType) => state[key as keyof StateType]; | ||
storeIn[`use${capitalize(key)}`] = () => storeIn(selector); | ||
}); | ||
|
||
return storeIn as UseBoundStore<StoreApi<StateType>> & | ||
ZustandHookSelectors<StateType>; | ||
} |
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 |
---|---|---|
@@ -1,42 +1,2 @@ | ||
import { State, UseStore } from 'zustand'; | ||
|
||
interface Selector<StoreType> { | ||
use: { | ||
[key in keyof StoreType]: () => StoreType[key]; | ||
}; | ||
} | ||
|
||
type Hook<BaseType> = { | ||
[Key in keyof BaseType as `use${Capitalize< | ||
string & Key | ||
>}`]: () => BaseType[Key]; | ||
}; | ||
|
||
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); | ||
|
||
export function createSelectorFunctions<StoreType extends State>( | ||
store: UseStore<StoreType> | ||
) { | ||
(store as any).use = {}; | ||
|
||
Object.keys(store.getState()).forEach((key) => { | ||
const selector = (state: StoreType) => state[key as keyof StoreType]; | ||
(store as any).use[key] = () => store(selector); | ||
}); | ||
|
||
return store as UseStore<StoreType> & Selector<StoreType>; | ||
} | ||
|
||
export function createSelectorHooks<StoreType extends State>( | ||
store: UseStore<StoreType> | ||
) { | ||
(store as any).use = {}; | ||
|
||
Object.keys(store.getState()).forEach((key) => { | ||
const selector = (state: StoreType) => state[key as keyof StoreType]; | ||
// @ts-ignore | ||
store[`use${capitalize(key)}`] = () => store(selector); | ||
}); | ||
|
||
return store as UseStore<StoreType> & Hook<StoreType>; | ||
} | ||
export * from './createSelectorFunctions'; | ||
export * from './createSelectorHooks'; |
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
Oops, something went wrong.