UNPKG

react-localstorage-hook

Version:

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

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