Function: useAgent()
typescript
1
useAgent(): AgentContextType & object
Defined in: hooks/index.ts:294
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 containing:
agent
: Current Agent instance or null if not initializedstart
: Async function to start the agent and begin operationsstop
: Async function to stop the agent and cleanup resourcesstate
: Current agent state string (stopped, starting, running, etc.)setAgent
: Function to manually set a new agent instance
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>);}