UNPKG

@reactutils/use-local-storage

Version:

A custom hook that provides a simple interface to the browser's local storage.

2 lines (1 loc) 420 B
import{useState as r}from"react";function t(t,e){var n=r((function(){if("undefined"==typeof window)return e;try{var r=window.localStorage.getItem(t);return r?JSON.parse(r):e}catch(r){return console.error(r),e}})),o=n[0],i=n[1];return[o,function(r){try{var e=r instanceof Function?r(o):r;i(e),"undefined"!=typeof window&&window.localStorage.setItem(t,JSON.stringify(e))}catch(r){console.error(r)}}]}export{t as default};