@react-hookz/web
Version:
React hooks done right, for browser and SSR.
38 lines (37 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLocalStorageValue = void 0;
var useStorageValue_1 = require("../useStorageValue/useStorageValue");
var const_1 = require("../util/const");
var IS_LOCAL_STORAGE_AVAILABLE = false;
try {
IS_LOCAL_STORAGE_AVAILABLE = const_1.isBrowser && !!window.localStorage;
}
catch (_a) {
// no need to test this flag leads to noop behaviour
/* istanbul ignore next */
IS_LOCAL_STORAGE_AVAILABLE = false;
}
/**
* Manages a single localStorage key.
*
* @param key Storage key to manage
* @param defaultValue Default value to yield in case the key is not in storage
* @param options
*/
exports.useLocalStorageValue = IS_LOCAL_STORAGE_AVAILABLE
? function (key, defaultValue, options) {
if (defaultValue === void 0) { defaultValue = null; }
if (options === void 0) { options = {}; }
return (0, useStorageValue_1.useStorageValue)(localStorage, key, defaultValue, options);
}
: function (key, defaultValue, options) {
if (defaultValue === void 0) { defaultValue = null; }
if (options === void 0) { options = {}; }
/* istanbul ignore next */
if (const_1.isBrowser && process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn('LocalStorage is not available in this environment');
}
return [undefined, const_1.noop, const_1.noop, const_1.noop];
};