0.0.42 • Published 4 years ago
@deepcase/store v0.0.42
deepcase store
interface
// js
const [value, setValue, unsetValue] = useSomeStore(keyInStorage, defaultValue);// ts
const [value, setValue, unsetValue]: [Type, (value: Type) => any, () => any] = useSomeStore<Type>(keyInStorage: string, defaultValue: Type);valueworks equal with useStoresetValue(newValue)works equal with useStore, setnewValuein selected store inkeyInStorageunsetValue()deletekeyInStoragefrom storekeyInStoragekey used in storagedefaultValueused as defaultvaluedata when storekeyInStorageis empty
usage
To use any hook, be sure to use the appropriate provider higher in the react tree.
import { QueryStoreProvider, useQueryStore } from '@deepcase/store/query';
import { CookiesStoreProvider, useCookiesStore } from '@deepcase/store/cookies';
import { LocalStoreProvider, useLocalStore } from '@deepcase/store/local';
import { CapacitorStoreProvider, useCapacitorStore } from '@deepcase/store/capacitor';<QueryStoreProvider>
<CookiesStoreProvider>
<LocalStoreProvider>
<CapacitorStoreProvider
fetchInterval={5000} {/* optional, disabled by default, need to support catching not @deepcase/store based capacitor store changes. */}
>
<Content/>
</CapacitorStoreProvider>
</LocalStoreProvider>
</CookiesStoreProvider>
</QueryStoreProvider>const [query, setQuery] = useQueryStore('demo', 5);
// ?demo=5
const [cookie, setCookie] = useCookiesStore('demo', 5);
// cookies demo=5
const [local, setLocal] = useLocalStore('demo', 5);
// localStorage.getItem('demo') // 5
const [capacitor, setCapacitor] = useCapacitorStore('demo', 5);
// await Storage.get('demo') // { value: 5 }compatibility
- useStore
- web
- android
- ios
- electron
- useCookiesStore
- web
- android
- ios
- electron
- useQueryStore
- web
- android
- ios
- electron
- useLocalStore
- web
- android
- ios
- electron
- useCapacitorStore
- web
- android
- ios
- electron
cases
prepare stores
// stores.tsx
export function useToken() {
return useCookiesStore('my-token-key', null);
}
// component.tsx
const [token, setToken] = useToken();provide contextual stores
// stores.tsx
import { IUseStore } from '@deepcase/store/store';
export const OptionsContext = React.createContext<IUseStore | void>();
export function OptionsProvider({ key, children }: { key: string; children: any }) {
const useStore = React.useMemo(() => {
return function useOptions(defualtValue) {
return useQueryStore(key, defualtValue);
};
}, []);
return <OptionsContext.Provider value={useStore}>{children}</OptionsContext.Provider>
}
export function useOptions(defualtValue) {
return React.useContext(OptionsContext)(defualtValue);
}
// component1.tsx
<OptionsProvider key={'abc'}><Component2/></OptionsProvider>
// component2.tsx
const [options, setOptions] = useOptions({ x: 'y' });0.0.40
4 years ago
0.0.41
4 years ago
0.0.42
4 years ago
0.0.38
4 years ago
0.0.39
4 years ago
0.0.37
4 years ago
0.0.35
4 years ago
0.0.36
4 years ago
0.0.34
4 years ago
0.0.33
5 years ago
0.0.32
5 years ago
0.0.30
5 years ago
0.0.31
5 years ago
0.0.27
5 years ago
0.0.28
5 years ago
0.0.29
5 years ago
0.0.26
5 years ago
0.0.25
5 years ago
0.0.24
5 years ago
0.0.23
5 years ago
0.0.22
5 years ago
0.0.21
5 years ago
0.0.20
5 years ago
0.0.19
5 years ago
0.0.18
5 years ago
0.0.16
5 years ago
0.0.17
5 years ago
0.0.15
5 years ago