Skip to content

Instantly share code, notes, and snippets.

@herbievine
Created November 1, 2021 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herbievine/f92cf4cb2161bc9aaa842af41dca6092 to your computer and use it in GitHub Desktop.
Save herbievine/f92cf4cb2161bc9aaa842af41dca6092 to your computer and use it in GitHub Desktop.
React Native custom theme selector - ./src/lib/storage.ts
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