React
Build real-time React applications with Rivet Actors
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.
Installation
Install the Rivet React package:
Basic Usage
Create Actor Registry
First, set up your actor registry (typically in your backend):
Set Up React Client
Create a typed client and Rivet hooks:
Use in Components
Connect to actors and listen for real-time updates:
API Reference
createRivetKit(client, options?)
Creates the Rivet hooks for React integration.
Parameters
client
: The Rivet client created withcreateClient
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 instanceparams
: Optional parameters passed to the actor connectionenabled
: Optional boolean to conditionally enable/disable the connection (default: true)
Returns
Actor object with the following properties:
connection
: The actor connection for calling actions, ornull
if not connectedisConnected
: Boolean indicating if the actor is connectedstate
: Current actor state (if available)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:
Authentication
Pass authentication parameters to actors:
Error Handling
Handle connection errors gracefully:
Custom Hooks
Create reusable custom hooks for common patterns:
Real-time Collaboration
Build collaborative features with multiple event listeners:
Client Connection Options
Basic Client Setup
Create a type-safe client to connect to your backend:
React Integration
Use the React hooks for seamless integration:
Environment Configuration
Development vs Production
Create environment-specific configurations:
Backend Configuration
Update your server to use environment-based configuration:
Frontend Environment Variables
Configure your frontend for different environments:
Authentication Integration
Protected Actors
Add authentication to secure your actors:
React Authentication
Connect authenticated actors in React:
Learn more about authentication.
Best Practices
- Use Custom Hooks: Extract actor logic into reusable custom hooks
- Handle Loading States: Always account for the initial loading state
- Error Boundaries: Implement error boundaries around actor components
- Conditional Connections: Use the
enabled
prop to control when actors connect - Event Cleanup: Event listeners are automatically cleaned up, but be mindful of heavy operations in handlers
- State Management: Combine with React state for local UI state that doesn't need to be shared