All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.cjs.react-utils.useLocalStorage.useLocalStorage.js.map Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
{"version":3,"file":"useLocalStorage.js","names":["_react","require","_ramda","_utils","getStorageValue","key","defaultValue","storedValue","localStorage","getItem","JSON","parse","error","logger","useLocalStorage","_useState","useState","_useState2","_slicedToArray2","setStoredValue","setValue","value","prevStoredValue","newValue","isNil","removeFromLocalStorage","setToLocalStorage","_default","exports"],"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":";;;;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AALA;;AAOA,IAAMG,eAAe,GAAG,SAAlBA,eAAeA,CAAIC,GAAG,EAAEC,YAAY,EAAK;EAC7C,IAAI;IACF,IAAMC,WAAW,GAAGC,YAAY,CAACC,OAAO,CAACJ,GAAG,CA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