Skip to content

feat: create useLocalStorage custom hook and migrate useTheme - #645

Merged
Onomebello merged 1 commit into
Navin-xmr:devfrom
Demilade10:issue#616
Jul 31, 2026
Merged

feat: create useLocalStorage custom hook and migrate useTheme#645
Onomebello merged 1 commit into
Navin-xmr:devfrom
Demilade10:issue#616

Conversation

@Demilade10

Copy link
Copy Markdown

Summary

Closes #616

Implements a type-safe, generic useLocalStorage<T> custom hook to replace direct localStorage access, and migrates useTheme.ts to use it.

Changes

frontend/src/hooks/useLocalStorage.ts (new)

  • Generic useLocalStorage<T>(key, initialValue) returning [storedValue, setValue, removeValue] as const
  • setValue accepts a direct value or an updater function (prev: T) => T
  • removeValue clears the key from localStorage and resets state to initialValue (never null)
  • All reads/writes in try/catch - corrupted/invalid JSON falls back to initialValue silently
  • SSR guard: typeof window === 'undefined' check before any window.localStorage access

frontend/src/hooks/useLocalStorage.test.ts (new)

10 tests covering:

  • Returns initialValue when key is absent
  • Reads an existing value from storage on mount
  • setValue with a direct value and with an updater function
  • Object serialisation round-trip
  • removeValue clears storage and resets state to initialValue (not null)
  • Graceful recovery from corrupted JSON (no throw, returns initialValue)

frontend/src/hooks/useTheme.ts (modified)

Replaced direct localStorage.getItem / localStorage.setItem calls with useLocalStorage. External API (useTheme, ThemeProvider, toggleTheme) is unchanged.

frontend/src/hooks/index.ts (modified)

Added: export { useLocalStorage } from './useLocalStorage'

Testing

pnpm exec vitest run src/hooks/useLocalStorage.test.ts 10/10 tests passed

All four touched files pass ESLint with zero errors or warnings.

Screenshots

Hooks-only change - no UI output.

@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@Demilade10 is attempting to deploy a commit to the dinahmaccodes' projects Team on Vercel.

A member of the Team first needs to authorize it.

@Onomebello
Onomebello merged commit 9886cd0 into Navin-xmr:dev Jul 31, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants