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

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

tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { 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>
);
}