React
The ActorCore React framework provides a convenient way to use ActorCore in React applications with standard React patterns.
Installation
Install the package:
Quick Start
API Reference
The React integration leverages React’s hooks system to provide an idiomatic way to interact with ActorCore in React applications.
createReactActorCore
The main function that creates React hooks for interacting with ActorCore. It takes a client instance and returns hook functions.
Parameters
client
: The ActorCore client created withcreateClient
.
Returns
An object containing React hooks:
useActor
: Hook for connecting to actorsuseActorEvent
: Hook for subscribing to actor events
useActor
Hook that connects to an actor, creating it if necessary. It manages the actor connection and returns the actor handle.
Parameters
actorName
: The name of the actor to connect to (string).options
: Optional connection options (same options asclient.actorName.get()
).id
: String identifier for the actor instance.tags
: Key-value pairs for actor identification.params
: Parameters to pass during connection.noCreate
: Boolean to prevent actor creation if it doesn’t exist.
Returns
Returns an array with a single object containing:
actor
: The actor handle if connected, orundefined
if still connecting.error
: Any error that occurred during connection.isLoading
: Boolean indicating if the connection is in progress.state
: String representing the internal connection state (“init”, “creating”, “created”, or “error”).
useActorEvent
Hook that subscribes to events from an actor.
Parameters
opts
: Object containing:actor
: The actor handle fromuseActor
, or undefined.event
: The name of the event to subscribe to.
cb
: Function called when the event is fired. The arguments passed to this function depend on the event type.
Returns
This hook doesn’t return a value. The subscription is automatically managed by the hook lifecycle.
Example Usage
Basic Counter Example
Error Handling
Dynamic Actor Selection
Next Steps
See the Interacting with Actors documentation for more information about the client API.