UNPKG

react-localstorage-hook

Version:

[![npm package][npm-badge]][npm] ![npm-bundle-size] ![Module formats][module-formats] ![npm-download-count]

2 lines (1 loc) 863 B
"use strict";var e=require("react"),n=function(n,t){var r=[];return function(){var u=function(n,t){var r=e.useState((function(){var e=localStorage.getItem(n);return null===e?t:JSON.parse(e)})),u=r[0],o=r[1],a=e.useCallback((function(e){o((function(){return localStorage.setItem(n,JSON.stringify(e)),e}))}),[n]);return e.useLayoutEffect((function(){var e=function(e){e.storageArea===localStorage&&e.key===n&&o(null===e.newValue?t:JSON.parse(e.newValue))};return window.addEventListener("storage",e),function(){return window.removeEventListener("storage",e)}})),[u,a]}(n,t),o=u[0],a=u[1],i=e.useCallback((function(e){for(var n=0,t=r;n<t.length;n++){(0,t[n])(e)}}),[]);return e.useLayoutEffect((function(){return r.push(a),function(){r.splice(r.indexOf(a),1)}}),[a]),[o,i]}},t={};module.exports=function(e,r){return void 0===r&&(r=""),e in t||(t[e]=n(e,r)),t[e]()};