Skip to content

Instantly share code, notes, and snippets.

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) => {
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