23
loading...
This website collects cookies to deliver better user experience
if (typeof window !== 'undefined') {
// localStorage code here
}
if (process.browser) {
// localStorage code here
}
{/*
`Storage` User will determine what storage object will he/she be using.
This way, user cant pass unnecessary string values
*/}
type StorageType = 'session' | 'local';
{/*
`UseStorageReturnValue` This is just a return value type for our hook.
We can add additional typings later.
*/}
type UseStorageReturnValue = {
getItem: (key: string, type?: StorageType) => string;
};
const useStorage = (): UseStorageReturnValue => {
const isBrowser: boolean = ((): boolean => typeof window !== 'undefined')();
const getItem = (key: string, type?: StorageType): string => {
const storageType: 'localStorage' | 'sessionStorage' = `${type ?? 'session'}Storage`;
return isBrowser ? window[storageType][key] : '';
};
return {
getItem,
};
};
export default useStorage;
import useStorage from 'hooks/useStorage';
const { getItem } = useStorage();
const token = getItem('token');
console.log(token); // will return either a <token-value> or <''>
type UseStorageReturnValue = {
getItem: (key: string, type?: StorageType) => string;
// you can set the return value to void or anything, as for my side, i just want to
// check if the value was saved or not
setItem: (key: string, value: string, type?: StorageType) => boolean;
};
getItem
code and reuse the storageType
variable inside. Here, we use it as a function.const useStorage = (): UseStorageReturnValue => {
const storageType = (type?: StorageType): 'localStorage' | 'sessionStorage' => `${type ?? 'session'}Storage`;
const isBrowser: boolean = ((): boolean => typeof window !== 'undefined')();
const getItem = (key: string, type?: StorageType): string => {
return isBrowser ? window[storageType(type)][key] : '';
};
const setItem = (key: string, value: string, type?: StorageType): boolean => {
if (isBrowser) {
window[storageType(type)].setItem(key, value);
return true;
}
return false;
};
UseStorageReturnValue
// We'll set this to a return type value of void since
// running the method always throws undefined
removeItem: (key: string, type?: StorageType) => void;
type StorageType = 'session' | 'local';
type UseStorageReturnValue = {
getItem: (key: string, type?: StorageType) => string;
setItem: (key: string, value: string, type?: StorageType) => boolean;
removeItem: (key: string, type?: StorageType) => void;
};
const useStorage = (): UseStorageReturnValue => {
const storageType = (type?: StorageType): 'localStorage' | 'sessionStorage' => `${type ?? 'session'}Storage`;
const isBrowser: boolean = ((): boolean => typeof window !== 'undefined')();
const getItem = (key: string, type?: StorageType): string => {
return isBrowser ? window[storageType(type)][key] : '';
};
const setItem = (key: string, value: string, type?: StorageType): boolean => {
if (isBrowser) {
window[storageType(type)].setItem(key, value);
return true;
}
return false;
};
const removeItem = (key: string, type?: StorageType): void => {
window[storageType(type)].removeItem(key);
};
return {
getItem,
setItem,
removeItem,
};
};
export default useStorage;