Overview
Zustand persist middleware rehydrates state from AsyncStorage on startup. If the stored JSON is malformed (truncated write, encoding error, schema version mismatch), JSON.parse throws and the store initializes with undefined state instead of initialState defaults. Components immediately crash with cannot-read-undefined errors. No recovery mechanism exists.
Specifications
Features:
- All persisted stores provide
onRehydrateStorage error callback resetting to initialState
- Hydration error logged to
appLogger.warn with store name
- Sentry receives warning event with store name after hydration failure
- User sees one-time toast after reset: 'Your preferences were reset'
Tasks:
- Add
onRehydrateStorage: () => (state, error) => { if (error) { set(initialState); appLogger.warn(...); } } to all persist configs
- Report to Sentry in error branch with store name
- Show toast on next render after any hydration error
- Add unit tests: malformed JSON input to each store verifies default state
- Include in Zustand store factory (from store factory issue)
Impacted Files:
- All
src/store/slices/*.ts files
src/store/createStore.ts (if store factory created)
Acceptance Criteria
- Malformed AsyncStorage JSON causes store to use default state, not crash
- Hydration error logged and sent to Sentry
- User notified with toast when any store resets to defaults
- Unit tests confirm default state on corrupted input for at least 3 stores
Overview
Zustand
persistmiddleware rehydrates state from AsyncStorage on startup. If the stored JSON is malformed (truncated write, encoding error, schema version mismatch),JSON.parsethrows and the store initializes withundefinedstate instead ofinitialStatedefaults. Components immediately crash with cannot-read-undefined errors. No recovery mechanism exists.Specifications
Features:
onRehydrateStorageerror callback resetting toinitialStateappLogger.warnwith store nameTasks:
onRehydrateStorage: () => (state, error) => { if (error) { set(initialState); appLogger.warn(...); } }to all persist configsImpacted Files:
src/store/slices/*.tsfilessrc/store/createStore.ts(if store factory created)Acceptance Criteria