UNPKG

react-use-localstorage

Version:
3 lines (2 loc) 658 B
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");exports.default=function(t,n){void 0===n&&(n="");var r=e.useState((function(){return window.localStorage.getItem(t)||n})),o=r[0],u=r[1];e.useEffect((function(){var e=window.localStorage.getItem(t);o!==e&&u(e||n)}));var a=e.useCallback((function(e){e.key===t&&e.newValue!==o&&u(e.newValue||n)}),[o]);return e.useEffect((function(){return window.addEventListener("storage",a),function(){return window.removeEventListener("storage",a)}}),[a]),[o,function(e){u(e),window.localStorage.setItem(t,e)}]}; //# sourceMappingURL=react-use-localstorage.cjs.production.min.js.map