@ic-reactor/react 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. package/README.md +39 -36
  2. package/dist/index.js +1 -1
  3. 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
- To use `@ic-reactor/react`, start by importing and setting up the `createReActor` function with your actor configurations:
29
+ Here's a simple example to get you started:
30
30
 
31
- ```javascript
32
- import createReActor from '@ic-reactor/react';
31
+ First, create an actor declaration file:
33
32
 
34
- const actorInitializer = /* Your actor initialization logic */;
35
- const reActorConfig = /* Your ReActor configuration options */;
33
+ ```js
34
+ // store.js
35
+ import { canisterId, createActor } from "declaration/actor"
36
+ import { createReActor } from "@ic-reactor/core"
36
37
 
37
- const { ReActorProvider, useReActorQuery, useReActorUpdate, ... } = createReActor(actorInitializer, reActorConfig);
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
- Finally, use the `useReActorQuery` and `useReActorUpdate` hooks to query and update actor state:
43
+ Wrap your app with the `ReActorProvider` component:
51
44
 
52
- ### Example: Querying Actor State
45
+ ```jsx
46
+ // App.jsx
53
47
 
54
- ```javascript
55
- const { recall, data, loading, error } = useQueryCall({
56
- functionName: "fetchData",
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
- ### Example: Updating Actor State
67
-
68
- ```javascript
69
- const { call, data, loading, error } = useUpdateCall({
70
- functionName: "updateData",
71
- args: ["arg1", "arg2"],
72
- })
73
-
74
- // Call the 'call' function to trigger an update call
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.1",
3
+ "version": "0.0.2",
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": "b1f673a150b591ad3cfbb8964794e4ea9b0ff1c2"
51
+ "gitHead": "948a6c0c4e176064907dc7e460033859d9d8090c"
52
52
  }