react native local storage code example
Example 1: react native storage
npm install react-native-storage
npm install @react-native-community/async-storage
Example 2: asyncstorage.getallkeys
importData = async () => {
try {
const keys = await AsyncStorage.getAllKeys();
const result = await AsyncStorage.multiGet(keys);
return result.map(req => JSON.parse(req)).forEach(console.log);
} catch (error) {
console.error(error)
}
}
Example 3: react-native app localisation
import memoize from 'lodash.memoize'; // Use for caching/memoize for better performanceimport i18n from 'i18n-js';import * as Localization from 'expo-localization';import { I18nManager } from 'react-native';export const translationGetters = { 'es-US': () => require('./es.json'), 'en-US': () => require('./en.json'), 'fr-FR': () => require('./fr.json'),};export const IMLocalized = memoize( (key, config) => i18n.t(key, config).includes('missing') ? key : i18n.t(key, config), (key, config) => (config ? key + JSON.stringify(config) : key),);export const init = () => { let localeLanguageTag = Localization.locale; let isRTL = Localization.isRTL; IMLocalized.cache.clear(); // update layout direction I18nManager.forceRTL(isRTL); // set i18n-js config i18n.translations = { [localeLanguageTag]: translationGetters[localeLanguageTag](), }; i18n.locale = localeLanguageTag;};