UNPKG

2.6 kBSource Map (JSON)View Raw
1{"version":3,"sources":["useLocalStorage.js"],"names":["errorMessageSSR","errorMessageIsNotSupported","useLocalStorage","localStorageKey","defaultValue","isClient","console","warn","JSON","stringify","undefined","window","localStorage","getItem","value","setValue","setItem"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;AAEA,IAAMA,eAAe,GAAG,+FAClB,qCADN;AAGA,IAAMC,0BAA0B,GAAG,qFAC7B,uCADN;;AAMA,IAAMC,eAAe,GAAG,SAAlBA,eAAkB,CAACC,eAAD,EAAkBC,YAAlB,EAAmC;AACzD,MAAI,CAACC,oBAAL,EAAe;AAEbC,IAAAA,OAAO,CAACC,IAAR,CAAaP,eAAb;AACA,WAAO,CAACQ,IAAI,CAACC,SAAL,CAAeL,YAAf,CAAD,EAA+B;AAAA,aAAMM,SAAN;AAAA,KAA/B,CAAP;AACD;;AAED,MAAI,CAAC,gCAAe,cAAf,CAAL,EAAqC;AAEnCJ,IAAAA,OAAO,CAACC,IAAR,CAAaN,0BAAb;AACA,WAAO,CAACO,IAAI,CAACC,SAAL,CAAeL,YAAf,CAAD,EAA+B;AAAA,aAAMM,SAAN;AAAA,KAA/B,CAAP;AACD;;AAXwD,kBAa/B,qBACxB,iCACEC,MAAM,CAACC,YAAP,CAAoBC,OAApB,CAA4BV,eAA5B,KACGK,IAAI,CAACC,SAAL,CAAeL,YAAf,CAFL,CADwB,CAb+B;AAAA;AAAA,MAalDU,KAbkD;AAAA,MAa3CC,QAb2C;;AAoBzD,wBAAU,YAAM;AACdJ,IAAAA,MAAM,CAACC,YAAP,CAAoBI,OAApB,CAA4Bb,eAA5B,EAA6CK,IAAI,CAACC,SAAL,CAAeK,KAAf,CAA7C;AACD,GAFD,EAEG,CAACX,eAAD,EAAkBW,KAAlB,CAFH;AAIA,SAAO,CAACA,KAAD,EAAQC,QAAR,CAAP;AACD,CAzBD;;eA2Beb,e","sourcesContent":["import { useState, useEffect } from 'react';\nimport safelyParseJson from './utils/safelyParseJson';\nimport isClient from './utils/isClient';\nimport isAPISupported from './utils/isAPISupported';\n\nconst errorMessageSSR = 'localStorage is not supported, this could happen because you\\'re using the useLocalStorage'\n + ' hook whilst server side rendering.';\n\nconst errorMessageIsNotSupported = 'localStorage is not supported, this could happen because window.localStorage is '\n + 'not supported by your current browser';\n\n/**\n * Save a value on local storage\n */\nconst useLocalStorage = (localStorageKey, defaultValue) => {\n if (!isClient) {\n // eslint-disable-next-line no-console\n console.warn(errorMessageSSR);\n return [JSON.stringify(defaultValue), () => undefined];\n }\n\n if (!isAPISupported('localStorage')) {\n // eslint-disable-next-line no-console\n console.warn(errorMessageIsNotSupported);\n return [JSON.stringify(defaultValue), () => undefined];\n }\n\n const [value, setValue] = useState(\n safelyParseJson(\n window.localStorage.getItem(localStorageKey)\n || JSON.stringify(defaultValue),\n ),\n );\n\n useEffect(() => {\n window.localStorage.setItem(localStorageKey, JSON.stringify(value));\n }, [localStorageKey, value]);\n\n return [value, setValue];\n};\n\nexport default useLocalStorage;\n"],"file":"useLocalStorage.js"}
\No newline at end of file