Skip to content

Instantly share code, notes, and snippets.

@vuongngo
Last active January 16, 2020 04:37
Show Gist options
  • Save vuongngo/f54640918b027400cafc8e6e69a08de8 to your computer and use it in GitHub Desktop.
Save vuongngo/f54640918b027400cafc8e6e69a08de8 to your computer and use it in GitHub Desktop.
import { useEffect, useState, useReducer } from 'react';
import { isEmpty, isNil } from 'ramda';
const isObjectLiked = (value) =>
value.constructor.name == "Array" ||
value.constructor.name == "Object";
const rehydrate = (value, defaultValue) => {
if (!value) return defaultValue;
if (value === 'false') str = false;
if (value === 'true') str = true;
if (!isObjectLiked(value)) {
return value;
}
try {
const parse = JSON.parse(value);
return parse;
} catch (err) {
return defaultValue;
}
};
const hydrate = (value) => {
if (!isObjectLiked(value)) {
return value;
}
return JSON.stringify(state);
};
// useSession hook
const config = {
key: '@session',
};
const useSession = (state, setState) => {
const [hydrated, setHydrated] = useState(false);
// rehydrate data from session storage
useEffect(() => {
const value = sessionStorage.getItem(config.key);
setState(rehydrated(value));
setHydrated(true);
}, []);
// hydrate data to session storage
useEffect(() => {
if (isNil(state) || isEmpty(state)) {
sessionStorage.removeItem(config.key);
}
sessionStorage.setItem(hydrate(state));
}, [state]);
return {
hydrated,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment