@react-hookz/web
Version:
React hooks done right, for browser and SSR.
35 lines (34 loc) • 1.34 kB
JavaScript
import { useStorageValue, } from "../useStorageValue/useStorageValue.js";
import { isBrowser, noop } from "../util/const.js";
var IS_SESSION_STORAGE_AVAILABLE = false;
try {
IS_SESSION_STORAGE_AVAILABLE = isBrowser && !!window.sessionStorage;
}
catch (_a) {
// no need to test this flag leads to noop behaviour
/* istanbul ignore next */
IS_SESSION_STORAGE_AVAILABLE = false;
}
/**
* Manages a single sessionStorage key.
*
* @param key Storage key to manage
* @param defaultValue Default value to yield in case the key is not in storage
* @param options
*/
export var useSessionStorageValue = IS_SESSION_STORAGE_AVAILABLE
? function (key, defaultValue, options) {
if (defaultValue === void 0) { defaultValue = null; }
if (options === void 0) { options = {}; }
return useStorageValue(sessionStorage, key, defaultValue, options);
}
: function (key, defaultValue, options) {
if (defaultValue === void 0) { defaultValue = null; }
if (options === void 0) { options = {}; }
/* istanbul ignore next */
if (isBrowser && process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn('SessionStorage is not available in this environment');
}
return [undefined, noop, noop, noop];
};