ic-reactor
    Preparing search index...

    Function createActorContext

    • Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain. This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.

      Type Parameters

      Parameters

      Returns CreateActorContextReturnType<A>

      An object containing the ActorProvider component and various hooks for interacting with the actor.

      • ActorProvider: A context provider component that allows child components to access and interact with the configured actor.
      • Hooks: Custom hooks derived from the actor context, facilitating interactions like querying or updating the actor's state.
      import React from 'react';
      import { createActorContext } from '@ic-reactor/react';
      import { backend, canisterId, idlFactory } from './declarations/candid'; // Assuming 'declarations/candid' is where your actor interface is defined.
      
      // Initialize the actor context with configuration options
      const { ActorProvider, useActorState, useQueryCall, useUpdateCall } = createActorContext({
        canisterId,
        idlFactory, // Optional, wrap the ActorProvider with CandidAdapterProvider
      });
      
      // A sample component that utilizes the actor context
      const App = () => (
        
          
            

      IC Actor Interaction Example

      ); export default App; // A sample component that uses the actor hooks const ActorComponent = () => { const { data, loading, error } = useQueryCall({ functionName: 'backendMethodName', args: [], refetchInterval: 10000, refetchOnMount: true, }); return (
      {loading &&

      Loading...

      } {error &&

      Error: {error.message}

      } {data &&

      Actor data: {data}

      }
      ); };

      This function streamlines the process of setting up a context for IC actor interactions within a React app, it provides a type-safe and efficient way to manage actor state and interactions.