useAgentState
useAgentState provides access to the agent’s initialization state and network information.
import { useAgentState } from "./reactor/hooks"
function AgentStatus() { const { isInitialized, // Agent is ready isInitializing, // Agent is starting up isLocalhost, // Connected to local network network, // "ic" | "local" | "remote" | undefined error, // Error | undefined } = useAgentState()
if (isInitializing) return <div>Connecting...</div> return <div>Connected to {network}</div>}Return Value
Section titled “Return Value”| Property | Type | Description |
|---|---|---|
isInitialized |
boolean |
Whether the agent has finished initializing |
isInitializing |
boolean |
Whether the agent is currently initializing |
isLocalhost |
boolean |
Whether the agent is connected to a development network: network is "local" or "remote" |
network |
string | undefined |
The network the agent is connected to — "ic", "local", or "remote" (a replica forwarded by Codespaces or Gitpod; a page on those domains gets it without a host option) |
error |
Error | undefined |
Any error that occurred during initialization |
Examples
Section titled “Examples”Initial Loading Screen
Section titled “Initial Loading Screen”import type { ReactNode } from "react"import { useAgentState } from "./reactor/hooks"
function App({ children }: { children: ReactNode }) { const { isInitializing, error } = useAgentState()
if (isInitializing) { return <div className="loading">Initializing Agent...</div> }
if (error) { return <div className="error">Failed to connect: {error.message}</div> }
return children}See Also
Section titled “See Also”- createAuthHooks — Auth hooks factory
- useAuth — Main auth hook