Hamdi Kahloun’s Post

View profile for Hamdi Kahloun, graphic

Mobile Development Manager

🚀 Excited to introduce a type-safe AsyncStorage wrapper for React Native! Simplify data management with TypeScript integration. Check out the snippet below: ```typescript import AsyncStorage from '@react-native-async-storage/async-storage'; interface StorageValue<T> { value: T | null; } class StorageService { static async setItem<T>(key: string, value: T): Promise<void> { try { const storageValue: StorageValue<T> = { value }; await AsyncStorage.setItem(key, JSON.stringify(storageValue)); } catch (error) { console.error('Error setting item:', error); } } static async getItem<T>(key: string): Promise<T | null> { try { const jsonValue = await AsyncStorage.getItem(key); const storageValue: StorageValue<T> = jsonValue != null ? JSON.parse(jsonValue) : { value: null }; return storageValue.value; } catch (error) { console.error('Error getting item:', error); return null; } } static async removeItem(key: string): Promise<void> { try { await AsyncStorage.removeItem(key); } catch (error) { console.error('Error removing item:', error); } } } // Example usage: // await StorageService.setItem<string>('username', 'JohnDoe'); // const username = await StorageService.getItem<string>('username'); // StorageService.removeItem('username'); ``` Simplify your React Native app's data management with confidence and efficiency using TypeScript's type-checking capabilities! 🛠️💡 #ReactNativeDevelopment #AsyncStorageWrapper #TypeScript #MobileDevelopmentTips

  • No alternative text description for this image

To view or add a comment, sign in

Explore topics