Function: useAgent()
typescript
1
useAgent(): object
Defined in: packages/identus-react/src/hooks/useAgent.ts:66
Hook for accessing the main Identus Agent context and operations.
The Agent is the central component that orchestrates all Identus SDK operations including DIDComm messaging, credential operations, and connection management. This hook must be used within an AgentProvider.
Returns
Agent context object with agent management functionality
agent
typescript
1
agent: null | Agent
Current Agent instance or null if not initialized
setAgent()
typescript
1
setAgent: (agent) => void
Function to manually set a new agent instance
Parameters
agent
Returns
void
start()
typescript
1
start: () => Promise<void>
Function to start the agent and begin operations
Returns
Promise<void>
state
typescript
1
state: State
Current agent state (stopped, starting, running, etc.)
stop()
typescript
1
stop: () => Promise<void>
Function to stop the agent and cleanup resources
Returns
Promise<void>
Throws
When used outside of AgentProvider
Example
tsx1234567891011121314151617181920212223242526272829303132333435363738import { useAgent } from '@trust0/identus-react/hooks';function AgentController() {const { agent, start, stop, state, setAgent } = useAgent();const handleStart = async () => {try {await start();console.log('Agent started successfully');} catch (error) {console.error('Failed to start agent:', error);}};const handleStop = async () => {try {await stop();console.log('Agent stopped successfully');} catch (error) {console.error('Failed to stop agent:', error);}};return (<div><p>Agent State: {state}</p><div><button onClick={handleStart} disabled={state === 'running'}>Start Agent</button><button onClick={handleStop} disabled={state === 'stopped'}>Stop Agent</button></div>{agent && <p>Agent ID: {agent.getCurrentDID()?.toString()}</p>}</div>);}