Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit e3e2a1b

Browse files
zashraf1985mjc1283
authored andcommitted
fix(event processor / pending events store): Added a check before accessing localstorage to make it work for React Native (optimizely#359)
Summary: React Native does not have localStorage. Added a check to make sure it does not throw error in react native. This is a temporary fix to make sure error is not thrown in React Native. Will implement a better fix in the future. Test plan: Manually tested thoroughly.
1 parent 274ef4b commit e3e2a1b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/event-processor/CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### Fixed
11+
- Fixed a runtime error when accessing localstorage in `PendingEventsStore` when SDK is used in a React Native application. Pending events will still not be stored when SDK is used in React Native but no error will be thrown anymore.
12+
1013
## [0.3.1] - August 29, 2019
1114

1215
### Fixed

packages/event-processor/src/pendingEventsStore.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export class LocalStorageStore<K extends StoreEntry> implements PendingEventsSto
7272

7373
replace(map: { [key: string]: K }): void {
7474
try {
75-
localStorage.setItem(this.LS_KEY, JSON.stringify(map))
75+
// This is a temporary fix to support React Native which does not have localStorage.
76+
window.localStorage && localStorage.setItem(this.LS_KEY, JSON.stringify(map))
7677
this.clean()
7778
} catch (e) {
7879
logger.error(e)
@@ -103,7 +104,8 @@ export class LocalStorageStore<K extends StoreEntry> implements PendingEventsSto
103104

104105
private getMap(): { [key: string]: K } {
105106
try {
106-
const data = localStorage.getItem(this.LS_KEY)
107+
// This is a temporary fix to support React Native which does not have localStorage.
108+
const data = window.localStorage && localStorage.getItem(this.LS_KEY);
107109
if (data) {
108110
return (JSON.parse(data) as { [key: string]: K }) || {}
109111
}

0 commit comments

Comments
 (0)