|
| 1 | +--- |
| 2 | +title: React |
| 3 | +icon: react |
| 4 | +--- |
| 5 | + |
| 6 | +The ActorCore React framework provides a convenient way to use ActorCore in React applications with standard React patterns. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Install the package: |
| 11 | + |
| 12 | +<CodeGroup> |
| 13 | +```sh npm |
| 14 | +npm add @actor-core/react |
| 15 | +``` |
| 16 | + |
| 17 | +```sh pnpm |
| 18 | +pnpm add @actor-core/react |
| 19 | +``` |
| 20 | + |
| 21 | +```sh yarn |
| 22 | +yarn add @actor-core/react |
| 23 | +``` |
| 24 | + |
| 25 | +```sh bun |
| 26 | +bun add @actor-core/react |
| 27 | +``` |
| 28 | +</CodeGroup> |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +```tsx |
| 33 | +import { createClient } from "actor-core/client"; |
| 34 | +import { createReactActorCore } from "@actor-core/react"; |
| 35 | +import type { App } from "../counter/src/index"; |
| 36 | +import React, { useState } from "react"; |
| 37 | + |
| 38 | +// Create a client |
| 39 | +const client = createClient<App>("http://your-actor-core-server.com"); |
| 40 | + |
| 41 | +// Create React hooks for your actors |
| 42 | +const { useActor, useActorEvent } = createReactActorCore(client); |
| 43 | + |
| 44 | +function ReactApp() { |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <Counter /> |
| 48 | + </> |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function Counter() { |
| 53 | + // Get or create an actor |
| 54 | + const [{ actor }] = useActor("counter"); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + <CounterValue actor={actor} /> |
| 59 | + <button |
| 60 | + type="button" |
| 61 | + onClick={() => actor?.increment(1)} |
| 62 | + disabled={!actor} |
| 63 | + > |
| 64 | + Increment |
| 65 | + </button> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function CounterValue({ actor }) { |
| 71 | + const [count, setCount] = useState(0); |
| 72 | + |
| 73 | + // Listen to events |
| 74 | + useActorEvent({ actor, event: "newCount" }, (newCount) => { |
| 75 | + setCount(newCount); |
| 76 | + }); |
| 77 | + |
| 78 | + return count; |
| 79 | +} |
| 80 | + |
| 81 | +render(<ReactApp />, document.getElementById("root")); |
| 82 | +``` |
| 83 | + |
| 84 | +## API Reference |
| 85 | + |
| 86 | +The React integration leverages React's hooks system to provide an idiomatic way to interact with ActorCore in React applications. |
| 87 | + |
| 88 | +### `createReactActorCore` |
| 89 | + |
| 90 | +The main function that creates React hooks for interacting with ActorCore. It takes a client instance and returns hook functions. |
| 91 | + |
| 92 | +```tsx |
| 93 | +const { useActor, useActorEvent } = createReactActorCore(client); |
| 94 | +``` |
| 95 | + |
| 96 | +#### Parameters |
| 97 | + |
| 98 | +- `client`: The ActorCore client created with `createClient`. |
| 99 | + |
| 100 | +#### Returns |
| 101 | + |
| 102 | +An object containing React hooks: |
| 103 | +- `useActor`: Hook for connecting to actors |
| 104 | +- `useActorEvent`: Hook for subscribing to actor events |
| 105 | + |
| 106 | +### `useActor` |
| 107 | + |
| 108 | +Hook that connects to an actor, creating it if necessary. It manages the actor connection and returns the actor handle. |
| 109 | + |
| 110 | +```tsx |
| 111 | +const [{ actor, error, isLoading, state }] = useActor(actorName, options); |
| 112 | +``` |
| 113 | + |
| 114 | +#### Parameters |
| 115 | + |
| 116 | +- `actorName`: The name of the actor to connect to (string). |
| 117 | +- `options`: Optional connection options (same options as `client.actorName.get()`). |
| 118 | + - `id`: String identifier for the actor instance. |
| 119 | + - `tags`: Key-value pairs for actor identification. |
| 120 | + - `params`: Parameters to pass during connection. |
| 121 | + - `noCreate`: Boolean to prevent actor creation if it doesn't exist. |
| 122 | + |
| 123 | +#### Returns |
| 124 | + |
| 125 | +Returns an array with a single object containing: |
| 126 | +- `actor`: The actor handle if connected, or `undefined` if still connecting. |
| 127 | +- `error`: Any error that occurred during connection. |
| 128 | +- `isLoading`: Boolean indicating if the connection is in progress. |
| 129 | +- `state`: String representing the internal connection state ("init", "creating", "created", or "error"). |
| 130 | + |
| 131 | +### `useActorEvent` |
| 132 | + |
| 133 | +Hook that subscribes to events from an actor. |
| 134 | + |
| 135 | +```tsx |
| 136 | +useActorEvent({ actor, event }, cb); |
| 137 | +``` |
| 138 | + |
| 139 | +#### Parameters |
| 140 | + |
| 141 | +- `opts`: Object containing: |
| 142 | + - `actor`: The actor handle from `useActor`, or undefined. |
| 143 | + - `event`: The name of the event to subscribe to. |
| 144 | +- `cb`: Function called when the event is fired. The arguments passed to this function depend on the event type. |
| 145 | + |
| 146 | +#### Returns |
| 147 | + |
| 148 | +This hook doesn't return a value. The subscription is automatically managed by the hook lifecycle. |
| 149 | + |
| 150 | +## Example Usage |
| 151 | + |
| 152 | +### Basic Counter Example |
| 153 | + |
| 154 | +```tsx |
| 155 | +import { createClient } from "actor-core/client"; |
| 156 | +import { createReactActorCore } from "@actor-core/react"; |
| 157 | +import type { App } from "./app"; |
| 158 | +import { useState } from "react"; |
| 159 | + |
| 160 | +const client = createClient<App>("http://localhost:6420"); |
| 161 | +const { useActor, useActorEvent } = createReactActorCore(client); |
| 162 | + |
| 163 | +function Counter() { |
| 164 | + // Connect to a counter actor with ID "main-counter" |
| 165 | + const [{ actor, isLoading }] = useActor("counter", { id: "main-counter" }); |
| 166 | + const [count, setCount] = useState(0); |
| 167 | + |
| 168 | + // Subscribe to counter updates |
| 169 | + useActorEvent({ actor, event: "newCount" }, (newCount) => { |
| 170 | + setCount(newCount); |
| 171 | + }); |
| 172 | + |
| 173 | + // Handle increment |
| 174 | + const handleIncrement = async () => { |
| 175 | + if (actor) { |
| 176 | + await actor.increment(1); |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + if (isLoading) return <div>Loading...</div>; |
| 181 | + |
| 182 | + return ( |
| 183 | + <div> |
| 184 | + <p>Current count: {count}</p> |
| 185 | + <button onClick={handleIncrement}>Increment</button> |
| 186 | + </div> |
| 187 | + ); |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +### Error Handling |
| 192 | + |
| 193 | +```tsx |
| 194 | +function Counter() { |
| 195 | + const [{ actor, error, isLoading }] = useActor("counter", { id: "main-counter" }); |
| 196 | + |
| 197 | + if (isLoading) return <div>Loading...</div>; |
| 198 | + if (error) return <div>Error: {error.message}</div>; |
| 199 | + if (!actor) return <div>Actor not available</div>; |
| 200 | + |
| 201 | + return ( |
| 202 | + <div> |
| 203 | + <button onClick={() => actor.increment(1)}>Increment</button> |
| 204 | + </div> |
| 205 | + ); |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +### Dynamic Actor Selection |
| 210 | + |
| 211 | +```tsx |
| 212 | +function CounterSelector() { |
| 213 | + const [counterId, setCounterId] = useState("counter-1"); |
| 214 | + const [{ actor }] = useActor("counter", { id: counterId }); |
| 215 | + |
| 216 | + return ( |
| 217 | + <div> |
| 218 | + <select |
| 219 | + value={counterId} |
| 220 | + onChange={(e) => setCounterId(e.target.value)} |
| 221 | + > |
| 222 | + <option value="counter-1">Counter 1</option> |
| 223 | + <option value="counter-2">Counter 2</option> |
| 224 | + <option value="counter-3">Counter 3</option> |
| 225 | + </select> |
| 226 | + |
| 227 | + {actor && <CounterDisplay actor={actor} />} |
| 228 | + </div> |
| 229 | + ); |
| 230 | +} |
| 231 | + |
| 232 | +function CounterDisplay({ actor }) { |
| 233 | + const [count, setCount] = useState(0); |
| 234 | + |
| 235 | + useActorEvent({ actor, event: "newCount" }, (newCount) => { |
| 236 | + setCount(newCount); |
| 237 | + }); |
| 238 | + |
| 239 | + return <div>Count: {count}</div>; |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +## Next Steps |
| 244 | + |
| 245 | +See the [Interacting with Actors](/concepts/interacting-with-actors) documentation for more information about the client API. |
0 commit comments