Skip to content

Instantly share code, notes, and snippets.

@SitaGomes
Last active March 23, 2024 02:11
Show Gist options
  • Save SitaGomes/6bdc0531fdaf40ffe87ed92a4e1ec33e to your computer and use it in GitHub Desktop.
Save SitaGomes/6bdc0531fdaf40ffe87ed92a4e1ec33e to your computer and use it in GitHub Desktop.
useLocalStorage
import { useState, useEffect, Dispatch, SetStateAction } from "react";
type Response<T> = [
T, //? What's inside the "state" Ex: string, boolean...
Dispatch<SetStateAction<T>> //? type of "setState"
]
//? "useLocalState<T>" accepts a type unknown
export function useLocalState<T>(key: string, initialState: T): Response<T> {
//? The return of "useLocalState" is a type of "Response<T>"
const [state, setState] = useState(() => {
let value
if(typeof window !== 'undefined')
value = localStorage.getItem(key) //? NextJs loads on server first
if (value) {
//? if the data is already set, just return it
return JSON.parse(value)
}
return initialState
})
useEffect(() => {
//? Keeping the "state" in JSON, so it's changed easyly
localStorage.setItem(key, JSON.stringify(state))
}, [key, state])
return [state, setState]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment