Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.