Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@illinois/react-use-local-storage

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@illinois/react-use-local-storage - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

2

lib/index.d.ts

@@ -1,2 +0,2 @@

declare const useLocalStorage: (key: any, initialValue: any) => any[];
declare const useLocalStorage: <T>(key: string, initialValue: T) => [T, (value: T) => void];
export default useLocalStorage;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
/*
const isClient = typeof window !== 'undefined'
const useLocalStorage = <T>(key: string, initialValue: T): [T, (value: T) => void] => {
if (!isClient) {
// We're SSRing; can't use local storage here!
return [initialValue, () => {}]
}
const [state, updateState] = useState((): T => {
try {
const localStorageValue = window.localStorage.getItem(key)
if (localStorageValue === null) {
// Initialize local storage with default state
window.localStorage.setItem(key, JSON.stringify(initialValue))
return initialValue
} else {
return JSON.parse(localStorageValue)
}
} catch {
// User might be facing storage restrictions, or JSON
// serialization/deserialization may have failed. We can just fall back
// to using React state here.
return initialValue
}
})
const localStorageChanged = (e: StorageEvent) => {
if (e.key === key) {
updateState(JSON.parse(e.newValue as string))
}
}
const setState = (value: T) => {
window.localStorage.setItem(key, JSON.stringify(value))
updateState(value)
}
useEffect(() => {
window.addEventListener('storage', localStorageChanged)
return () => {
window.removeEventListener('storage', localStorageChanged)
}
})
return [state, setState]
}
*/
const isClient = typeof window !== 'undefined';
const useLocalStorage = (key, initialValue) => {
if (window.localStorage.getItem(key) === null) {
window.localStorage.setItem(key, JSON.stringify(initialValue));
if (!isClient) {
// We're SSRing; can't use local storage here!
return [initialValue, () => { }];
}
const localStorageValue = JSON.parse(window.localStorage.getItem(key));
const [state, updateState] = react_1.useState(localStorageValue);
const localStorageChanged = e => {
const [state, updateState] = react_1.useState(() => {
try {
const localStorageValue = window.localStorage.getItem(key);
if (localStorageValue === null) {
// Initialize local storage with default state
window.localStorage.setItem(key, JSON.stringify(initialValue));
return initialValue;
}
else {
return JSON.parse(localStorageValue);
}
}
catch (_a) {
// User might be facing storage restrictions, or JSON
// serialization/deserialization may have failed. We can just fall back
// to using React state here.
return initialValue;
}
});
const localStorageChanged = (e) => {
if (e.key === key) {

@@ -58,3 +34,3 @@ updateState(JSON.parse(e.newValue));

};
const setState = value => {
const setState = (value) => {
window.localStorage.setItem(key, JSON.stringify(value));

@@ -61,0 +37,0 @@ updateState(value);

{
"name": "@illinois/react-use-local-storage",
"version": "1.0.0",
"version": "1.0.1",
"description": "React hook that persists and syncs state with local storage",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc