Skip to content

Instantly share code, notes, and snippets.

View ekrresa's full-sized avatar
🎯
chilling...

Ochuko Ekrresa ekrresa

🎯
chilling...
View GitHub Profile
@ekrresa
ekrresa / useLocalStorage.tsx
Last active December 29, 2023 00:21
Custom react hook to manage localStorage values. SSR safe
export function useLocalStorage<T>(key: string, initialValue: T) {
const [value, setValue] = React.useState<T | undefined>(() => initialValue);
// Gets initial value from localStorage if available
React.useLayoutEffect(() => {
let initialValue;
try {
const localStorageValue = localStorage.getItem(key);