UNPKG

@reactutils/use-local-storage

Version:

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

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