Skip to content

fix(hooks): Guard useLocalStorage against invalid JSON#1536

Open
YianZhao wants to merge 2 commits intoeclipse-sw360:mainfrom
YianZhao:fix/localstorage-json-guard
Open

fix(hooks): Guard useLocalStorage against invalid JSON#1536
YianZhao wants to merge 2 commits intoeclipse-sw360:mainfrom
YianZhao:fix/localstorage-json-guard

Conversation

@YianZhao
Copy link
Copy Markdown

Summary

This PR hardens useLocalStorage against malformed JSON values stored in localStorage.

Related Issue

Fixes #1535

Changes

  • Added safe parser helper inside useEffect
  • Wrapped JSON.parse in try/catch
  • Fallback to initialValue on parse failure
  • Remove invalid storage key to prevent repeated failures

Signed-off-by: Alex <alextanzhao22@gmail.com>
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.

useLocalStorage crashes when localStorage contains malformed JSON

2 participants