Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 531aa6a

Browse files
committed
prepare examples
1 parent 482fd7b commit 531aa6a

File tree

15 files changed

+1871
-170
lines changed

15 files changed

+1871
-170
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("@rnx-kit/eslint-plugin/recommended");

example/.eslintrc.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/App.tsx

Lines changed: 0 additions & 59 deletions
This file was deleted.

example/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
/**
2-
* @format
3-
*/
4-
51
import {AppRegistry} from 'react-native';
6-
import App from './App';
2+
import App from './src/App';
73
import {name as appName} from './app.json';
84

95
AppRegistry.registerComponent(appName, () => App);

example/metro.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const path = require('path');
77
* @type {import('metro-config').MetroConfig}
88
*/
99
const config = {
10-
watchFolders: [
11-
path.resolve(__dirname, "..")
12-
],
10+
watchFolders: [path.resolve(__dirname, '..')],
1311
resolver: {
1412
nodeModulesPaths: [path.resolve(__dirname, 'node_modules')],
1513
unstable_enableSymlinks: true,

example/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"scripts": {
66
"android": "react-native run-android",
77
"ios": "react-native run-ios",
8-
"lint": "eslint .",
98
"start": "react-native start",
109
"test": "jest"
1110
},
@@ -17,14 +16,12 @@
1716
"@babel/core": "^7.20.0",
1817
"@babel/preset-env": "^7.20.0",
1918
"@babel/runtime": "^7.20.0",
20-
"@react-native/eslint-config": "^0.72.2",
2119
"@react-native/metro-config": "^0.72.11",
2220
"@tsconfig/react-native": "^3.0.0",
2321
"@types/react": "^18.0.24",
2422
"@types/react-test-renderer": "^18.0.0",
2523
"babel-jest": "^29.2.1",
2624
"babel-plugin-module-resolver": "^5.0.0",
27-
"eslint": "^8.19.0",
2825
"jest": "^29.2.1",
2926
"metro-react-native-babel-preset": "0.76.8",
3027
"prettier": "^2.4.1",

example/src/App.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, {useState} from 'react';
2+
import {SafeAreaView, ScrollView, StatusBar, View} from 'react-native';
3+
import useDatabase from './hooks/useDatabase';
4+
import DatabaseSelector from './components/DatabaseSelector';
5+
import BasicCrud from './components/BasicCrud';
6+
7+
function App(): JSX.Element {
8+
const [dbName, setDbName] = useState('example_db.db');
9+
const database = useDatabase(dbName);
10+
return (
11+
<SafeAreaView>
12+
<StatusBar barStyle={'dark-content'} />
13+
<ScrollView contentContainerStyle={{width: '100%', height: '100%'}}>
14+
<View style={{flex: 1}}>
15+
<DatabaseSelector
16+
initialName={dbName}
17+
onCreateDatabase={newName => setDbName(newName)}
18+
/>
19+
<BasicCrud key={dbName} db={database} />
20+
</View>
21+
</ScrollView>
22+
</SafeAreaView>
23+
);
24+
}
25+
26+
export default App;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import {useState} from 'react';
3+
import {Button, StyleSheet, TextInput, View} from 'react-native';
4+
5+
type Props = {
6+
initialName: string;
7+
children?: React.ReactElement;
8+
onCreateDatabase: (dbName: string) => void;
9+
};
10+
11+
const DatabaseSelector: React.FC<Props> = ({
12+
children,
13+
initialName,
14+
onCreateDatabase,
15+
}) => {
16+
const [input, setInput] = useState<string>(initialName);
17+
18+
function createDatabase() {
19+
if (!input) {
20+
return;
21+
}
22+
onCreateDatabase(input);
23+
}
24+
25+
return (
26+
<View>
27+
<View style={styles.row}>
28+
<TextInput style={styles.input} onChangeText={setInput} value={input} />
29+
<View style={styles.buttons}>
30+
<Button title="Select database" onPress={createDatabase} />
31+
</View>
32+
</View>
33+
{children}
34+
</View>
35+
);
36+
};
37+
38+
const styles = StyleSheet.create({
39+
row: {
40+
padding: 8,
41+
flexDirection: 'row',
42+
justifyContent: 'space-around',
43+
alignItems: 'center',
44+
},
45+
input: {
46+
paddingHorizontal: 8,
47+
borderStyle: 'solid',
48+
borderWidth: 1,
49+
flex: 1,
50+
borderColor: 'gray',
51+
},
52+
buttons: {alignItems: 'center', justifyContent: 'center', padding: 8},
53+
});
54+
55+
export default DatabaseSelector;

example/src/hooks/useDatabase.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, {useEffect} from 'react';
2+
import {AsyncStorageSqlite} from '@react-native-async-storage/sqlite-storage';
3+
4+
function useDatabase(dbName: string) {
5+
const [database, setDatabase] = React.useState<AsyncStorageSqlite>(() => {
6+
return new AsyncStorageSqlite(dbName);
7+
});
8+
9+
useEffect(() => {
10+
if (dbName && database?.name !== dbName) {
11+
setDatabase(new AsyncStorageSqlite(dbName));
12+
}
13+
}, [dbName]);
14+
15+
return database;
16+
}
17+
18+
export default useDatabase;

0 commit comments

Comments
 (0)