1.7.2 • Published 7 days ago

@ic-reactor/react v1.7.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 days ago

@ic-reactor/react is a comprehensive React library designed to streamline interactions with the Internet Computer (IC) blockchain. It provides React hooks and utilities for efficient state management, authentication, and interaction with IC actors.

Features

  • React Hooks Integration: Custom hooks for managing blockchain actor states and authentication within React applications.
  • Type-Safe Actor Interactions: Type-safe interaction with IC actors using the provided actor declaration file.
  • Efficient State Management: Utilize the power of Zustand for global state management in React components.
  • Asynchronous Data Handling: Easy handling of asynchronous operations related to IC actors.
  • Authentication Support: Integrated hooks for managing authentication with the IC blockchain.
  • Auto-Refresh and Query Capabilities: Support for auto-refreshing data and querying IC actors.

Installation

Install the package using npm:

npm install @ic-reactor/react

or using yarn:

yarn add @ic-reactor/react

Usage

Here's a simple example to get you started:

First, create an actor declaration file:

// actor.ts
import { canisterId, idlFactory, actor } from "declaration/actor"
import { createReactor } from "@ic-reactor/react"

type Actor = typeof actor

export const { useActorStore, useAuth, useQueryCall } = createReactor<Actor>({
  canisterId,
  idlFactory,
  host: "https://localhost:4943",
})

Then, use the useQueryCall hook to call your canister method:

// Balance.tsx
import { useQueryCall } from "./actor"

const Balance = ({ principal }) => {
  const { call, data, loading, error } = useQueryCall({
    functionName: "get_balance",
    args: [principal],
    refetchInterval: 1000,
    refetchOnMount: true,
    onLoading: () => console.log("Loading..."),
    onSuccess: (data) => console.log("Success!", data),
    onError: (error) => console.log("Error!", error),
  })

  return (
    <div>
      <button onClick={call} disabled={loading}>
        {loading ? "Loading..." : "Refresh"}
      </button>
      {loading && <p>Loading...</p>}
      {data && <p>Balance: {data}</p>}
      {error && <p>Error: {error}</p>}
    </div>
  )
}

export default Balance

Authentication

@ic-reactor/react provides a custom hook for managing authentication with the IC blockchain. To use it, first create an authentication declaration file:

// Login.tsx
import { useAuth } from "./actor"

const Login = () => {
  const {
    login,
    logout,
    loginLoading,
    loginError,
    identity,
    authenticating,
    authenticated,
  } = useAuth()

  return (
    <div>
      <h2>Login:</h2>
      <div>
        {loginLoading && <div>Loading...</div>}
        {loginError ? <div>{JSON.stringify(loginError)}</div> : null}
        {identity && <div>{identity.getPrincipal().toText()}</div>}
      </div>
      {authenticated ? (
        <div>
          <button onClick={logout}>Logout</button>
        </div>
      ) : (
        <div>
          <button onClick={login} disabled={authenticating}>
            Login
          </button>
        </div>
      )}
    </div>
  )
}

export default Login
1.7.2

7 days ago

1.7.1

8 days ago

1.7.0

9 days ago

1.6.6

14 days ago

1.6.5

14 days ago

1.6.4

15 days ago

1.6.3

18 days ago

1.6.2

19 days ago

1.6.1

19 days ago

1.6.0

19 days ago

1.6.0-beta.7

19 days ago

1.6.0-beta.4

21 days ago

1.6.0-beta.5

21 days ago

1.6.0-beta.6

21 days ago

1.6.0-beta.0

21 days ago

1.6.0-beta.1

21 days ago

1.6.0-beta.2

21 days ago

1.6.0-beta.3

21 days ago

1.5.5-beta.2

22 days ago

1.5.5-beta.1

23 days ago

1.5.5-beta.0

25 days ago

1.5.4

26 days ago

1.5.3

27 days ago

1.5.2

29 days ago

1.5.1

1 month ago

1.5.0

1 month ago

1.4.4

1 month ago

1.4.3

1 month ago

1.4.2

1 month ago

1.4.1

1 month ago

1.4.0

2 months ago

1.3.3

2 months ago

1.3.2

2 months ago

1.3.1

2 months ago

1.3.0

2 months ago

1.2.3

2 months ago

1.2.0

2 months ago

1.2.2

2 months ago

1.2.1

2 months ago

1.1.7

2 months ago

1.1.6

2 months ago

1.1.5

2 months ago

1.1.4

2 months ago

1.1.1

3 months ago

1.1.3

2 months ago

1.1.2

2 months ago

1.0.9

3 months ago

1.0.8

3 months ago

1.0.7

3 months ago

1.0.6

3 months ago

1.0.5

3 months ago

1.1.0

3 months ago

1.0.10

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago

0.5.5

4 months ago

0.5.4

4 months ago

0.5.3

4 months ago

0.5.2

4 months ago

0.5.1

4 months ago

0.4.5

4 months ago

0.4.4

4 months ago

0.4.6

4 months ago

0.4.1

4 months ago

0.4.0

4 months ago

0.4.3

4 months ago

0.4.2

4 months ago

0.3.6

4 months ago

0.3.8

4 months ago

0.3.7

4 months ago

0.3.0

4 months ago

0.3.5

4 months ago

0.3.2

4 months ago

0.3.1

4 months ago

0.3.4

4 months ago

0.3.3

4 months ago

0.2.11

4 months ago

0.2.10

4 months ago

0.2.9

4 months ago

0.2.8

4 months ago

0.2.7

4 months ago

0.2.6

4 months ago

0.2.5

4 months ago

0.2.4

5 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.2.3

5 months ago

0.2.2

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago

0.0.10

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.5

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago