@ic-reactor/react 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -36
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -26,52 +26,55 @@ yarn add @ic-reactor/react
|
|
|
26
26
|
|
|
27
27
|
## Usage
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Here's a simple example to get you started:
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
import createReActor from '@ic-reactor/react';
|
|
31
|
+
First, create an actor declaration file:
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
```js
|
|
34
|
+
// store.js
|
|
35
|
+
import { canisterId, createActor } from "declaration/actor"
|
|
36
|
+
import { createReActor } from "@ic-reactor/core"
|
|
36
37
|
|
|
37
|
-
const { ReActorProvider,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Then, wrap your React application with the `ReActorProvider` component:
|
|
41
|
-
|
|
42
|
-
```javascript
|
|
43
|
-
const App = () => (
|
|
44
|
-
<ReActorProvider>
|
|
45
|
-
<YourApp />
|
|
46
|
-
</ReActorProvider>
|
|
38
|
+
export const { ReActorProvider, useQueryCall } = createReActor((agent) =>
|
|
39
|
+
createActor(canisterId, { agent })
|
|
47
40
|
)
|
|
48
41
|
```
|
|
49
42
|
|
|
50
|
-
|
|
43
|
+
Wrap your app with the `ReActorProvider` component:
|
|
51
44
|
|
|
52
|
-
|
|
45
|
+
```jsx
|
|
46
|
+
// App.jsx
|
|
53
47
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
args: ["arg1", "arg2"],
|
|
58
|
-
autoRefresh: true, // default: true
|
|
59
|
-
refreshInterval: 3000, // default: 5000
|
|
60
|
-
disableInitialCall: false, // default: false
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
// Use 'data', 'loading', 'error' in your component
|
|
48
|
+
<ReActorProvider>
|
|
49
|
+
<Balance principal={principal} />
|
|
50
|
+
</ReActorProvider>
|
|
64
51
|
```
|
|
65
52
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
|
|
53
|
+
Then, use the `useQueryCall` hook to call your canister method:
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
// Balance.jsx
|
|
57
|
+
import { useQueryCall } from "./store"
|
|
58
|
+
|
|
59
|
+
const Balance = ({ principal }) => {
|
|
60
|
+
const { recall, data, loading, error } = useQueryCall({
|
|
61
|
+
functionName: "get_balance",
|
|
62
|
+
args: [principal],
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div>
|
|
67
|
+
<button onClick={() => recall()} disabled={loading}>
|
|
68
|
+
{loading ? "Loading..." : "Refresh"}
|
|
69
|
+
</button>
|
|
70
|
+
{loading && <p>Loading...</p>}
|
|
71
|
+
{data && <p>Balance: {data}</p>}
|
|
72
|
+
{error && <p>Error: {error}</p>}
|
|
73
|
+
</div>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default Balance
|
|
75
78
|
```
|
|
76
79
|
|
|
77
80
|
## API Reference
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,7 @@ const react_1 = require("react");
|
|
|
18
18
|
const hooks_1 = require("./hooks");
|
|
19
19
|
const defaultCreateReActorOptions = {
|
|
20
20
|
initializeOnMount: true,
|
|
21
|
-
host: process.env.NODE_ENV === "production"
|
|
21
|
+
host: process.env.NODE_ENV === "production" || process.env.DFX_NETWORK === "ic"
|
|
22
22
|
? "https://icp-api.io"
|
|
23
23
|
: "http://localhost:4943",
|
|
24
24
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "A React library for interacting with Dfinity actors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"@peculiar/webcrypto": "1.4",
|
|
49
49
|
"react": ">=16.8"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "b64c094f66a177bd410f9231e229b6f6da24f081"
|
|
52
52
|
}
|