|
| 1 | +import type {AsyncStorageSqlite} from '@react-native-async-storage/sqlite-storage'; |
| 2 | +import React, {useState} from 'react'; |
| 3 | +import {Button, StyleSheet, Text, View} from 'react-native'; |
| 4 | + |
| 5 | +type Props = {db: AsyncStorageSqlite}; |
| 6 | + |
| 7 | +const BasicCrud: React.FC<Props> = ({db}) => { |
| 8 | + const [value, setValue] = useState<null | string>(null); |
| 9 | + |
| 10 | + async function readValue() { |
| 11 | + const stored = await db.read('random'); |
| 12 | + setValue(JSON.stringify(stored, null, 2)); |
| 13 | + } |
| 14 | + |
| 15 | + async function createAndSaveRandomValue() { |
| 16 | + const randomValue = (Math.random() % 100) * 100; |
| 17 | + await db.save('random', String(randomValue)); |
| 18 | + await readValue(); |
| 19 | + } |
| 20 | + |
| 21 | + async function deleteValue() { |
| 22 | + await db.delete('random'); |
| 23 | + await readValue(); |
| 24 | + } |
| 25 | + |
| 26 | + return ( |
| 27 | + <View style={styles.container}> |
| 28 | + <Text style={styles.header}>Basic crud example</Text> |
| 29 | + <View style={styles.row}> |
| 30 | + <Button title="read random" onPress={readValue} /> |
| 31 | + <Button title="Save random" onPress={createAndSaveRandomValue} /> |
| 32 | + <Button title="delete random" onPress={deleteValue} /> |
| 33 | + </View> |
| 34 | + |
| 35 | + <Text>{value ?? 'null'}</Text> |
| 36 | + </View> |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +const styles = StyleSheet.create({ |
| 41 | + container: { |
| 42 | + padding: 8, |
| 43 | + }, |
| 44 | + header: { |
| 45 | + fontSize: 18, |
| 46 | + }, |
| 47 | + row: { |
| 48 | + paddingVertical: 18, |
| 49 | + flexDirection: 'row', |
| 50 | + justifyContent: 'space-between', |
| 51 | + alignItems: 'center', |
| 52 | + }, |
| 53 | +}); |
| 54 | + |
| 55 | +export default BasicCrud; |
0 commit comments