npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ic-use-actor

v0.0.9

Published

React Hook and context provider to make interacting with Internet Computer canisters more fun!

Downloads

48

Readme

ic-use-actor

A React context provider for managing Internet Computer (IC) actors with enhanced features like type safety and request/response interceptors. ic-use-actor makes interacting with Internet Computer canisters more fun!

version downloads

Features

  • Shared Actor Context: Allows the same actor to be used across multiple components.
  • Typescript Support: Makes full use of the canister service definitions to provide type safety for requests and responses.
  • Interceptors: onRequest, onResponse, onRequestError, and onResponseError callbacks allow for intercepting and processing requests and responses.

Table of Contents

Pre-requisites

ic-use-actor needs an Internet Computer (IC) identity to work. The examples below uses ic-use-siwe-identity as an identity provider. You can use any other identity provider as long as it returns a valid IC identity.

Installation

npm install ic-use-actor @dfinity/agent @dfinity/candid

Usage

To use ic-use-actor in your React application, follow these steps:

1. Setting Up the Actor Context and Hook

First, create an actor context and a corresponding hook for each IC canister you would like to access. Export the hook to be able to use it in your components. The hook returned by createUseActorHook can be named anything you want. If using ic-use-actor with multiple canisters, you might want to name the hook after the canister to make it easier to identify which hook is for which canister - for example, useMyCanister, useMyOtherCanister, etc.

import {
  createActorContext,
  createUseActorHook,
} from "ic-use-actor";
import { _SERVICE } from "path-to/your-service.did";

const actorContext = createActorContext<_SERVICE>();
export const useActor = createUseActorHook<_SERVICE>(actorContext);

2. Creating an Actor Provider Component

Create one or more ActorProvider components to provide access to your canisters. ActorProviders can be nested to provide access to multiple canisters.

// Actors.tsx

import { ReactNode } from "react";
import {
  ActorProvider,
  createActorContext,
  createUseActorHook,
} from "ic-use-actor";
import {
  canisterId,
  idlFactory,
} from "path-to/your-service/index";
import { _SERVICE } from "path-to/your-service.did";
import { useSiweIdentity } from "ic-use-siwe-identity";

const actorContext = createActorContext<_SERVICE>();
export const useActor = createUseActorHook<_SERVICE>(actorContext);

export default function Actors({ children }: { children: ReactNode }) {
  const { identity } = useSiweIdentity();

  return (
    <ActorProvider<_SERVICE>
      canisterId={canisterId}
      context={actorContext}
      identity={identity}
      idlFactory={idlFactory}
    >
      {children}
    </ActorProvider>
  );
}

3. Wrapping Your Application

Wrap your application root component with the ActorProvider component(s) you created in the previous step to provide access to your canisters.

// App.tsx

import Actors from "./Actors";

function App() {
  return (
    <Actors>
      <MyApplication />
    </Actors>
  );
}

4. Accessing the Actor in Components

In your components, use the useActor hook to access the actor:

// AnyComponent.tsx

import { useActor } from "path-to/useActor";

function AnyComponent() {
  const { actor } = useActor();

  // Use the actor for calling methods on your canister
  React.useEffect(() => {
    actor
      .my_method()
      .then((result) => {
        // Do something with the result
      })
      .catch((error) => {
        // Handle the error
      });
  }, []);
}

Advanced Usage

Setting up interceptors

Interceptors can be used to intercept requests and responses. You can use them to modify requests, log requests and responses, or perform other actions.

export default function Actor({ children }: { children: ReactNode }) {
  const { identity } = useSiweIdentity();

  const handleRequest = (data: InterceptorRequestData) => {
    // Do something
    // data: { args: unknown[], methodName: string }
    return data.args;
  };

  const handleResponse = (data: InterceptorResponseData) => {
    // Do something
    // data: { args: unknown[], methodName: string, response: unknown }
    return data.response;
  };

  const handleError = (data: InterceptorErrorData) => {
    // Do something
    // data: { args: unknown[], methodName: string, error: unknown }
    return data.error;
  };

  return (
    <ActorProvider<_SERVICE>
      canisterId={canisterId}
      context={actorContext}
      identity={identity}
      idlFactory={idlFactory}
      onRequest={handleRequest}
      onRequestError={handleError}
      onResponse={handleResponse}
      onResponseError={handleError}
    >
      {children}
    </ActorProvider>
  );
}

Updates

See the CHANGELOG for details on updates.

Contributing

Contributions are welcome. Please submit your pull requests or open issues to propose changes or report bugs.

License

This project is licensed under the MIT License. See the LICENSE file for more details.