- {error && (
-
- Error: {error}
-
- )}
-
- Status: {counter.isConnected ? "Connected" : "Disconnected"}
-
- {/* Rest of component */}
-
- );
-}
-```
-
-### Custom Hooks
-
-Create reusable custom hooks for common patterns:
-
-```tsx
-// Custom hook for a counter with persistent state
-function useCounter(counterId: string) {
- const [count, setCount] = useState(0);
-
- const counter = useActor({
- name: "counter",
- key: [counterId]
- });
-
- counter.useEvent("countChanged", setCount);
-
- const increment = useCallback(async (amount = 1) => {
- await counter.connection?.increment(amount);
- }, [counter.connection]);
-
- const reset = useCallback(async () => {
- await counter.connection?.reset();
- }, [counter.connection]);
-
- return {
- count,
- increment,
- reset,
- isConnected: counter.isConnected
- };
-}
-
-// Usage
-function App() {
- const { count, increment, reset, isConnected } = useCounter("my-counter");
-
- return (
-