Optional
context: any<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
.
Optional
propUsed 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.
Optional
contextOptional
defaultUsed to define default values for the props accepted by the component.
type Props = { name?: string }
const MyComponent: FC<Props> = (props) => {
return <div>{props.name}</div>
}
MyComponent.defaultProps = {
name: 'John Doe'
}
Optional
displayUsed in debugging messages. You might want to set it explicitly if you want to display a different name for debugging purposes.
const MyComponent: FC = () => {
return <div>Hello!</div>
}
MyComponent.displayName = 'MyAwesomeComponent'
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 fromcreateActorContext
is used.idlFactory
(optional): IDL.InterfaceFactory - The IDL factory for the actor interface. If not provided, the default fromcreateActorContext
is used.didjsId
(optional): string - The DID.js ID for authentication. If not provided, the default fromcreateActorContext
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:
canisterId
. Throws an error if it is missing, ensuring that a valid canister ID is always used for actor operations.useMemo
to combine the default configuration with the props provided to theActorProvider
, optimizing for performance by avoiding unnecessary recalculations.useActor
hook to initiate actor interactions based on the combined configuration, managing states such asfetching
,fetchError
, and the actorhooks
.loadingComponent
orfetchError
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.