React
Caution!
This integration is unstable and not recommended for production use. If you're looking for a stable integration, please let us know by upvoting the issue on our GitHub repository.
Getting Started
First, add Rivet's Actor Client SDK to your project.
Generate a Actor Manager endpoint by using the rivet
CLI and guide here. Then add the generated URL to your env file.
Create React App / Vite
If you're using Create React App or Vite, you can put ActorClientProvider
in App.tsx
.
Next.js Pages Router
If you're using Next.js Pages Router you can put ActorClientProvider
in _app.tsx
, or in the page where you're going to use actors.
Next.js App Router
If you're using Next.js app router you can put ActorClientProvider
in layout.tsx
, or in the layout where you're going to use actors.
We recommend creating a new component for your providers as shown below, because this SDK needs to be initialized on the client side with use client
directive.
Usage
Creating an Actor
useActor
is a hook that allows you to interact with actors in your React components. Pass to it a name of your actor, and optionally any other create parameters. It will connect to the actor, once the component is mounted, and disconnect when the component is unmounted.
The hook also provides the actor's state, error, and loading status. Actor created by useActor
is available to that particular component tree only. If you call useActor
somewhere else (even with the same arguments), it will create a new actor instance. To share the actor between components, you can use React's context or a state management library. It's safe to pass actor
instance to child components as a prop, as it does not change between renders.
Optionally, if you're using TypeScript, you can pass your Actor class type to the hook to get the correct types for RPC methods.
Remote Procedure Call (RPC)
To call any defined Remote Procedure Call (RPC) method on the actor, you can use the actor.methodName
syntax. Make sure to replace methodName
with the name of the method you want to call. Be aware that those methods are asynchronous and return a promise.
Events
To subscribe to actor events changes, you can use the actor.on
method.
Troubleshooting
JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists
If you're getting this error, you need to add types for React. Normally React does not come with types, so you need to tell Deno to use the types from the different package.
Advanced Usage
Server Driven UI
You can use the Actor RPC methods to return React components. This is done by using React Server Components paradigm.
In order to render components from your Actor, you need to ensure that your Actor extends the RscActor
class from the @rivet-gg/actor
package. Your RPC methods can receive props like any other React component.
Then, you can use the second return value of the useActor
hook to use your RscActor's elements. Make sure to wrap those elements in Suspense
.
Experimental Features
Caution!
Methods described below are unstable and may change in the future, do not use them in production.
Manual RSC update
If you need to re-render the component when the something happens in your actor, use this._updateRsc()
method. This will trigger a re-render of the RSC elements in your actor. Behind the scenes, this method broadcasts an internal event __rsc
that all RSC elements are listening to.
unstable_createActorHooks
unstable_createActorHooks
is a function that allows you to create type-safe hooks for your actors. It takes an actor name and returns a hook that can be used to interact with that actor. This is useful when you want to create a custom hook that encapsulates the logic for a specific actor and its types.
useActor
useActor
is a hook that allows you to interact with actors in your React components. Pass to it a name of your actor, and optionally any other create parameters. It will connect to the actor, once the component is mounted, and disconnect when the component is unmounted.
useActorEventCallback
useActorEventCallback
is a hook that allows you to subscribe to actor events changes. It takes an event name and a callback function that will be called when the event is triggered. You do not need to wrap the callback in useCallback
as the hook will handle that for you.
useActorRsc
useActorRsc
is a hook that allows you to use React Server Components from your actor. It takes an actor instance and returns a function that can be used to render the RSC elements. Make sure to wrap those elements in Suspense. The second return value of the hook is the function to refetch the RSC elements.
Note
This hook won't re-render the component when the actor state changes. If you need to re-render the component when the actor state changes. Use the second return value of the hook to refetch the response.