Function ActorProvider

  • ActorProvider is a React functional component that serves as a context provider for IC actor interactions within a React application. It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.

    Props:

    • canisterId (optional): string - The Canister ID for actor interactions. If not provided, the default from createActorContext is used.
    • idlFactory (optional): IDL.InterfaceFactory - The IDL factory for the actor interface. If not provided, the default from createActorContext is used.
    • didjsId (optional): string - The DID.js ID for authentication. If not provided, the default from createActorContext is used.
    • loadingComponent (optional): React Node - A component displayed during the loading/fetching state. Defaults to a simple message.
    • authenticatingComponent (optional): React Node - A component displayed during the authentication state. Defaults to a simple message.
    • children: React Node - The child components that will have access to the actor context.

    Behavior:

    • Validates the presence of a canisterId. Throws an error if it is missing, ensuring that a valid canister ID is always used for actor operations.
    • Utilizes useMemo to combine the default configuration with the props provided to the ActorProvider, optimizing for performance by avoiding unnecessary recalculations.
    • Employs the useActor hook to initiate actor interactions based on the combined configuration, managing states such as fetching, fetchError, and the actor hooks.
    • Conditionally renders the loadingComponent or fetchError based on the actor fetching state. Once the actor is ready and no errors are present, it renders the child components, effectively providing them access to the actor context.

    Parameters

    Returns ReactNode

    Example

    <ActorProvider canisterId="your-canister-id" idlFactory={yourIdlFactory}>
    <YourComponent />
    </ActorProvider>

    This setup ensures that YourComponent and any of its children can interact with the specified IC actor through the context provided by ActorProvider.

Properties

propTypes?: WeakValidationMap<ActorProviderProps>

Used to declare the types of the props accepted by the component. These types will be checked during rendering and in development only.

We recommend using TypeScript instead of checking prop types at runtime.

contextTypes?: ValidationMap<any>

Deprecated

Lets you specify which legacy context is consumed by this component.

See

Legacy React Docs

defaultProps?: Partial<ActorProviderProps>

Used to define default values for the props accepted by the component.

See

React Docs

Example

type Props = { name?: string }

const MyComponent: FC<Props> = (props) => {
return <div>{props.name}</div>
}

MyComponent.defaultProps = {
name: 'John Doe'
}

Deprecated

Use values for destructuring assignments instead.

displayName?: string

Used in debugging messages. You might want to set it explicitly if you want to display a different name for debugging purposes.

See

Legacy React Docs

Example


const MyComponent: FC = () => {
return <div>Hello!</div>
}

MyComponent.displayName = 'MyAwesomeComponent'