1.0.0-alpha.4 • Published 5 years ago
@react-typed-hooks/use-local-storage v1.0.0-alpha.4
useLocalStorage
React hook that will return the entry in localstorage for the passed in key after passing it through JSON.parse.
The hook also returns a setter that will update the returned value and store the result of JSON.stringify in localstorage
Installation
npm install @react-typed-hooks/use-local-storageUsage
Get a value out of localstorage
import { useLocalStorage } from "@react-typed-hooks/use-window-size";
const Demo = () => {
const [value] = useLocalStorage("any-key");
...
};Pass in a default value to return if the key is not found in localstorage.
import { useLocalStorage } from "@react-typed-hooks/use-window-size";
const Demo = () => {
const [value, setValue] = useLocalStorage("any-key", { foo: "bar" });
...
};Set a value in localstorage.
import { useLocalStorage } from "@react-typed-hooks/use-window-size";
const Demo = () => {
const [value, setValue] = useLocalStorage("any-key", "any-value");
return <button onClick={() => setValue("new-value")}>{value}</button>;
};API
Types
type SetValue = (value: any | ((value: any) => any)) => void;
function useLocalStorage(key: string, defaultValue: any): [any, SetValue];