-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstore.tsx
More file actions
30 lines (23 loc) · 818 Bytes
/
store.tsx
File metadata and controls
30 lines (23 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { createContext, Context, useContext } from 'react';
export interface IUseStoreSetHandler<T> {
(oldValue: T): T;
};
export interface IUseStore <T>{
(key: string, defaultValue: T): [T, (value: (T | IUseStoreSetHandler<T>)) => any, () => any, boolean];
}
export interface IStoreContext<T = any> {
useStore: IUseStore<T>;
}
export const defaultContext: IStoreContext = {
useStore: (key, defaultValue) => [defaultValue, value => undefined, () => undefined, true],
};
export const StoreContext = createContext<IStoreContext>(defaultContext);
export interface ISetKeyValue {}
export function useStore<T>(
key: string,
defaultValue: T,
context: Context<IStoreContext> = StoreContext,
): ReturnType<IUseStore<T>>{
const { useStore } = useContext(context);
return useStore(key, defaultValue);
}