Languages & Frameworks

React

Learn how to create real-time, stateful React applications with Rivet's actor model. The React integration provides intuitive hooks for managing actor connections and real-time updates.

Getting Started

See the React quickstart guide for getting started.

API Reference

createRivetKit(endpoint?, options?)

Creates the Rivet hooks for React integration.

Parameters

  • endpoint: Optional endpoint URL (defaults to http://localhost:6420 or process.env.RIVET_ENDPOINT)
  • options: Optional configuration object

Returns

An object containing:

  • useActor: Hook for connecting to actors

useActor(options)

Hook that connects to an actor and manages the connection lifecycle.

Parameters

  • options: Object containing:
    • name: The name of the actor type (string)
    • key: Array of strings identifying the specific actor instance
    • params: Optional parameters passed to the actor connection
    • createWithInput: Optional input to pass to the actor on creation
    • createInRegion: Optional region to create the actor in if does not exist
    • enabled: Optional boolean to conditionally enable/disable the connection (default: true)

Returns

Actor object with the following properties:

  • connection: The actor connection for calling actions, or null if not connected
  • connStatus: The connection status ("idle", "connecting", "connected", or "disconnected")
  • error: Error object if the connection failed, or null
  • useEvent(eventName, handler): Method to subscribe to actor events

actor.useEvent(eventName, handler)

Subscribe to events emitted by the actor.

Parameters

  • eventName: The name of the event to listen for (string)
  • handler: Function called when the event is emitted

Lifecycle

The event subscription is automatically managed:

  • Subscribes when the actor connects
  • Cleans up when the component unmounts or actor disconnects
  • Re-subscribes on reconnection

Advanced Patterns

Multiple Actors

Connect to multiple actors in a single component:

Conditional Connections

Control when actors connect using the enabled option:

Real-time Collaboration

Build collaborative features with multiple event listeners:

Authentication

Connect authenticated actors in React:

Learn more about authentication.

API Reference

Package: @rivetkit/react

See the full React API documentation at rivetkit.org/docs/actors/clients.