-
Notifications
You must be signed in to change notification settings - Fork 0
/
WasmComponent.js
55 lines (48 loc) · 1.32 KB
/
WasmComponent.js
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
import React from 'react';
// supposing an "add.wasm" module that exports a single function "add"
const WasmComponent = () => {
const [wasmModule, setWasmModule] = React.useState();
const loadWasm = async () => {
// try {
console.log("wasm before",new Date())
const wasm = await import('rust');
setWasmModule({ wasm });
console.log('wasm set',new Date());
callFetch(wasm);
//} catch (err) {
// console.error(`Unexpected error in loadWasm. [Message: ${err.message}]`);
//}
};
const callFetch = async (wasm) => {
console.log('calling fetch');
//const res = await wasm.greet("jj");
const resy = await wasm.k("127.0.0.1:9944");
console.log("HHHHHHHHHHHHHHHHHH",resy);
}
/*
// load wasm asynchronously
wasmModule === undefined && loadWasm();
if (wasmModule !== undefined) {
callFetch(wasmModule);
}
*/
wasmModule === undefined && loadWasm();
return (
<div className="App">
<header className="App-header">
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default WasmComponent;