-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.tsx
94 lines (76 loc) · 2.46 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useEffect, useState } from "react";
import { StyleSheet, Text, View } from "react-native";
// Option 1
// import Account from "@tasit/account";
// Option 2
// import { useAccount } from "@tasit/hooks";
// Option 3
import { hooks } from "tasit";
const { useAccount } = hooks;
import * as Random from "expo-random";
// ...
export default function App() {
///
// Option 1: Use vanilla React hooks + @tasit/account
///
// useEffect(() => {
// async function makeAccount() {
// const randomBytes = await Random.getRandomBytesAsync(16);
// const account = Account.createUsingRandomness(randomBytes);
// console.log({ account })
// const { address: accountAddress, privateKey } = account;
// console.log({ accountAddress });
// console.log({ privateKey });
// }
// makeAccount();
// }, []); // Just run this once
///
// Option 2: Use the useAccount hook from @tasit/hooks
///
// Note: If your app has a single data store like redux or if it uses
// Apollo which internally has a single data store, then you could use
// a useReducer hook (in the case of redux) or a useMutation hook
// (in the case of Apollo) rather than using useState here.
const [randomBytes, setRandomBytes] = useState(new Uint8Array());
// const [randomBytesGenerated, setRandomBytesGenerated] = useState(false);
console.log("New render");
console.log({ randomBytes });
console.log("randomBytes.length");
console.log(randomBytes.length);
useEffect(() => {
let isMounted = true;
async function makeRandomBytes() {
const randomBytesThatWereGenerated = await Random.getRandomBytesAsync(16);
if (isMounted) {
console.log("randomBytes generated");
setRandomBytes(randomBytesThatWereGenerated);
}
}
makeRandomBytes();
return () => {
isMounted = false;
};
}, []); // Just run this once
const randomBytesGenerated = randomBytes.length !== 0;
const address = useAccount({
randomBytes,
randomBytesGenerated,
});
console.log({ address });
const addressDefined = address !== "";
return (
<View style={styles.container}>
<Text>{addressDefined ? "Ready" : "Not ready"}</Text>
<Text>{randomBytesGenerated ? "Generated" : "Not generated"}</Text>
<Text>{address}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});