Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created February 10, 2019 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dance2die/004c227bd94d574c313cde52868e9cf5 to your computer and use it in GitHub Desktop.
Save dance2die/004c227bd94d574c313cde52868e9cf5 to your computer and use it in GitHub Desktop.
import React from "react";
export default function useLocalStorage(key: string, initialValue: string = "") {
const [item, setValue] = React.useState(() => {
const value = localStorage.getItem(key) || initialValue;
localStorage.setItem(key, value);
return value;
});
const setItem = (item: string) => {
setValue(item);
window.localStorage.setItem(key, item);
};
return [item, setItem];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment