package.react-utils.useLocalStorage.useLocalStorage.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
{"version":3,"file":"useLocalStorage.js","names":["useState","isNil","removeFromLocalStorage","setToLocalStorage","getStorageValue","key","defaultValue","storedValue","localStorage","getItem","JSON","parse","error","logger","useLocalStorage","_useState","_useState2","_slicedToArray","setStoredValue","setValue","value","prevStoredValue","newValue"],"sources":["../../../src/react-utils/useLocalStorage/useLocalStorage.js"],"sourcesContent":["/* eslint-disable @bigbinary/neeto/no-local-storage */\n\nimport { useState } from \"react\";\n\nimport { isNil } from \"ramda\";\nimport { removeFromLocalStorage, setToLocalStorage } from \"utils\";\n\nconst getStorageValue = (key, defaultValue) => {\n try {\n const storedValue = localStorage.getItem(key);\n\n return storedValue ? JSON.parse(storedValue) : defaultValue;\n } catch (error) {\n logger.error(error);\n\n return defaultValue;\n }\n};\n\nconst useLocalStorage = (key, defaultValue) => {\n const [storedValue, setStoredValue] = useState(() =>\n getStorageValue(key, defaultValue)\n );\n\n const setValue = value =>\n setStoredValue(prevStoredValue => {\n const newValue =\n typeof value === \"function\" ? value(prevStoredValue) : value;\n\n if (isNil(newValue)) {\n removeFromLocalStorage(key);\n } else {\n setToLocalStorage(key, newValue);\n }\n\n return newValue;\n });\n\n return [storedValue, setValue];\n};\n\nexport default useLocalStorage;\n"],"mappings":";AAAA;;AAEA,SAASA,QAAQ,QAAQ,OAAO;AAEhC,SAASC,KAAK,QAAQ,OAAO;AAC7B,SAASC,sBAAsB,EAAEC,iBAAiB;AAElD,IAAMC,eAAe,GAAG,SAAlBA,eAAeA,CAAIC,GAAG,EAAEC,YAAY,EAAK;EAC7C,IAAI;IACF,IAAMC,WAAW,GAAGC,YAAY,CAACC,OAAO,CAACJ,GAAG,CAAC;IAE7C,OAAOE,WAAW,GAAGG,IAAI,CAACC,K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