Files
botsmann/lib/hooks/useLocalStorage.ts
T
g-butandClaude Opus 4.6 0ae19d22a2 refactor(hooks): extract useLocalStorageFlag hook for dismiss state
Created lib/hooks/useLocalStorage.ts with SSR-safe boolean persistence.
Updated ChatAuthCTA and OnboardingChecklist to use it instead of raw
localStorage calls.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 17:23:13 +01:00

32 lines
717 B
TypeScript

'use client';
import { useState, useEffect, useCallback } from 'react';
/**
* Hook for persisting boolean state in localStorage.
* SSR-safe: returns initialValue until hydration completes.
*/
export function useLocalStorageFlag(
key: string,
initialValue = false,
): [boolean, (value: boolean) => void] {
const [value, setValue] = useState(initialValue);
useEffect(() => {
const stored = localStorage.getItem(key);
if (stored !== null) {
setValue(stored === 'true');
}
}, [key]);
const setAndPersist = useCallback(
(newValue: boolean) => {
setValue(newValue);
localStorage.setItem(key, String(newValue));
},
[key],
);
return [value, setAndPersist];
}