@devboldly/react-use-local-storage
Advanced tools
Comparing version 0.0.1 to 0.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var useLocalStorageItem_1 = require("./useLocalStorageItem"); | ||
var encode = function (val) { | ||
return val !== null ? (val ? 'true' : 'false') : 'null'; | ||
}; | ||
var decode = function (val) { | ||
return val !== null ? (val === 'null' ? null : val !== 'false') : val; | ||
}; | ||
function useLocalStorageBoolean(keyName, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, encode, decode); | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, useLocalStorageItem_1.defaultEncode, useLocalStorageItem_1.defaultDecode); | ||
} | ||
exports.useLocalStorageBoolean = useLocalStorageBoolean; |
export declare function useLocalStorageItem<T>(keyName: string, defaultValue: T | null | undefined, encode: (val: LocalStorageValue<T>) => string, decode: (itemString: string | null) => LocalStorageValue<T>): LocalStorageItem<T>; | ||
export declare type LocalStorageValue<T> = T | null; | ||
export declare type LocalStorageItem<T> = [LocalStorageValue<T>, (value: LocalStorageValue<T>) => void, () => void, () => void]; | ||
export declare function defaultEncode<T>(val: LocalStorageValue<T>): string; | ||
export declare function defaultDecode<T>(itemString: string | null): T; |
@@ -14,13 +14,6 @@ "use strict"; | ||
var _a = React.useState(true), firstRender = _a[0], setFirstRender = _a[1]; | ||
var _b = React.useState(false), shouldRestore = _b[0], setShouldRestore = _b[1]; | ||
var _c = React.useState(defaultValue), itemValue = _c[0], setValue = _c[1]; | ||
var _b = React.useState(false), shouldPush = _b[0], setShouldPush = _b[1]; | ||
var _c = React.useState(false), shouldPull = _c[0], setShouldPull = _c[1]; | ||
var _d = React.useState(defaultValue), itemValue = _d[0], setValue = _d[1]; | ||
React.useEffect(function () { | ||
var defaultValueString; | ||
try { | ||
defaultValueString = encode(defaultValue); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
return; | ||
} | ||
if (localStorage) { | ||
@@ -32,10 +25,10 @@ var retrievedLocalStorageState = localStorage.getItem(keyName); | ||
try { | ||
// If on first render we actually have a param, use it. | ||
// If on first render we actually find a value, pull it. | ||
if (retrievedLocalStorageState !== null) { | ||
setValue(decode(retrievedLocalStorageState)); | ||
setShouldPull(true); | ||
} | ||
// Else if we don't have a param state but a default state exists, use it. | ||
else if (defaultValueString !== null) { | ||
// Else if we didn't find a value but a default one was provided, push it. | ||
else if (defaultValue !== null) { | ||
setValue(defaultValue); | ||
localStorage.setItem(keyName, defaultValueString); | ||
setShouldPush(true); | ||
} | ||
@@ -47,15 +40,14 @@ } | ||
} | ||
// Restore value from localStorage when requested | ||
else if (shouldRestore) { | ||
setShouldRestore(false); | ||
} | ||
}, [keyName, firstRender, defaultValue]); | ||
React.useEffect(function () { | ||
if (localStorage) { | ||
var retrievedLocalStorageState = localStorage.getItem(keyName); | ||
// Pull value from localStorage | ||
if (shouldPull) { | ||
setShouldPull(false); | ||
try { | ||
// Fall back to default when null | ||
var newVal = retrievedLocalStorageState !== null ? decode(retrievedLocalStorageState) : defaultValue; | ||
var encodedVal = encode(newVal); | ||
setValue(newVal); | ||
if (newVal !== null && retrievedLocalStorageState === null) { | ||
// If localStorage item was set to null and there's a non-null default, | ||
// restore it back to localStorage | ||
localStorage.setItem(keyName, encodedVal); | ||
} | ||
} | ||
@@ -66,4 +58,5 @@ catch (e) { | ||
} | ||
// Synchronize localStorage with value thereafter | ||
else if (retrievedLocalStorageState !== itemValue) { | ||
// Push value to localStorage | ||
else if (shouldPush) { | ||
setShouldPush(false); | ||
if (itemValue !== null) { | ||
@@ -83,3 +76,3 @@ try { | ||
} | ||
}, [defaultValue, firstRender, keyName, shouldRestore, itemValue, decode, encode]); | ||
}, [keyName, shouldPull, shouldPush, decode, defaultValue, itemValue, encode]); | ||
var setItemValue = function (newVal) { | ||
@@ -89,6 +82,7 @@ if (localStorage) { | ||
setValue(newValOrDefault); | ||
setShouldPush(true); | ||
} | ||
}; | ||
var restore = function () { | ||
setShouldRestore(true); | ||
setShouldPull(true); | ||
}; | ||
@@ -101,1 +95,9 @@ var reset = function () { | ||
exports.useLocalStorageItem = useLocalStorageItem; | ||
function defaultEncode(val) { | ||
return JSON.stringify(val); | ||
} | ||
exports.defaultEncode = defaultEncode; | ||
function defaultDecode(itemString) { | ||
return itemString !== null ? JSON.parse(itemString) : null; | ||
} | ||
exports.defaultDecode = defaultDecode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var useLocalStorageItem_1 = require("./useLocalStorageItem"); | ||
var encode = function (val) { | ||
return val + ''; | ||
}; | ||
var decode = function (val) { | ||
return val !== null ? Number.parseFloat(val) : val; | ||
}; | ||
function useLocalStorageNumber(keyName, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, encode, decode); | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, useLocalStorageItem_1.defaultEncode, useLocalStorageItem_1.defaultDecode); | ||
} | ||
exports.useLocalStorageNumber = useLocalStorageNumber; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var useLocalStorageItem_1 = require("./useLocalStorageItem"); | ||
var encode = function (val) { | ||
var encodedVal = 'null'; | ||
try { | ||
encodedVal = JSON.stringify(val); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
return encodedVal; | ||
}; | ||
var decode = function (val) { | ||
var itemValue = null; | ||
try { | ||
itemValue = val !== null ? JSON.parse(val) : null; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
return itemValue; | ||
}; | ||
function useLocalStorageObject(keyName, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, encode, decode); | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, useLocalStorageItem_1.defaultEncode, useLocalStorageItem_1.defaultDecode); | ||
} | ||
exports.useLocalStorageObject = useLocalStorageObject; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var useLocalStorageItem_1 = require("./useLocalStorageItem"); | ||
var encode = function (val) { return (val !== null ? val : 'null'); }; | ||
var decode = function (val) { return val; }; | ||
function useLocalStorageString(keyName, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = null; } | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, encode, decode); | ||
return useLocalStorageItem_1.useLocalStorageItem(keyName, defaultValue, useLocalStorageItem_1.defaultEncode, useLocalStorageItem_1.defaultDecode); | ||
} | ||
exports.useLocalStorageString = useLocalStorageString; |
{ | ||
"name": "@devboldly/react-use-local-storage", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Dev Boldly <devboldly@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "React hook for accessing the localStorage API.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13485
156