|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import { |
| 3 | + SafeAreaView, |
| 4 | + StyleSheet, |
| 5 | + ScrollView, |
| 6 | + Text, |
| 7 | + StatusBar, |
| 8 | +} from 'react-native'; |
| 9 | +import MMKV from 'react-native-mmkv-storage'; |
| 10 | + |
| 11 | +const MMKVStorage = new MMKV.Loader().withEncryption().initialize(); |
| 12 | + |
| 13 | +interface StoredMap { |
| 14 | + name: string; |
| 15 | + date: number; |
| 16 | +} |
| 17 | + |
| 18 | +const App = () => { |
| 19 | + const [stringValue, setStringValue] = useState(''); |
| 20 | + const [arrayValue, setArrayValue] = useState<Array<undefined>>([]); |
| 21 | + const [mapValue, setMapValue] = useState<StoredMap>({ |
| 22 | + name: '', |
| 23 | + date: Date.now(), |
| 24 | + }); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const map = MMKVStorage.getMap<StoredMap>('map'); |
| 28 | + map && setMapValue(map); |
| 29 | + MMKVStorage.removeItem('map'); |
| 30 | + MMKVStorage.setMap('map', { |
| 31 | + name: 'AAA', |
| 32 | + date: Date.now(), |
| 33 | + }); |
| 34 | + const randomString = MMKVStorage.getString('random-string'); |
| 35 | + randomString && setStringValue(randomString); |
| 36 | + MMKVStorage.setString( |
| 37 | + 'random-string', |
| 38 | + Math.random().toString(36).substring(7), |
| 39 | + ); |
| 40 | + const array = MMKVStorage.getArray<undefined>('array'); |
| 41 | + array && setArrayValue(array); |
| 42 | + MMKVStorage.setArray('array', new Array(16).fill(undefined)); |
| 43 | + }, []); |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <StatusBar barStyle="dark-content" /> |
| 48 | + <SafeAreaView> |
| 49 | + <ScrollView |
| 50 | + contentInsetAdjustmentBehavior="automatic" |
| 51 | + style={styles.scrollView}> |
| 52 | + <Text>Previous value: {stringValue}</Text> |
| 53 | + <Text> |
| 54 | + Map value: {mapValue?.name} {mapValue?.date} |
| 55 | + </Text> |
| 56 | + <Text>Array Length: {arrayValue?.length}</Text> |
| 57 | + </ScrollView> |
| 58 | + </SafeAreaView> |
| 59 | + </> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +const styles = StyleSheet.create({ |
| 64 | + scrollView: { |
| 65 | + padding: 32, |
| 66 | + }, |
| 67 | +}); |
| 68 | + |
| 69 | +export default App; |
0 commit comments