Created
November 1, 2021 17:23
-
-
Save herbievine/f92cf4cb2161bc9aaa842af41dca6092 to your computer and use it in GitHub Desktop.
React Native custom theme selector - ./src/lib/storage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AsyncStorage from '@react-native-async-storage/async-storage' | |
import { Platform } from 'react-native' | |
const os = Platform.OS | |
const webStorage = window.localStorage | |
const appStorage = AsyncStorage | |
const getItem = async (key: string) => { | |
if (key) { | |
return os === 'web' | |
? webStorage.getItem(key) | |
: await appStorage.getItem(key) | |
} | |
return null | |
} | |
const setItem = async (key: string, payload: string) => { | |
if (key && payload) { | |
return os === 'web' | |
? webStorage.setItem(key, payload) | |
: await appStorage.setItem(key, payload) | |
} | |
return null | |
} | |
export { getItem, setItem } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment