Skip to content

Commit

Permalink
feat(rtc): add useIsConnected
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Mar 24, 2023
1 parent bf3c869 commit ee3aa21
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/agora-rtc-react/src/hooks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ export function useConnectionState(client?: IAgoraRTCClient | null): ConnectionS
return connectionState;
}

export function useIsConnected(client?: IAgoraRTCClient | null): boolean {
const [isConnected, setConnected] = useState(
client ? client.connectionState === "CONNECTED" : false,
);
useEffect(() => {
if (client) {
setConnected(client.connectionState === "CONNECTED");
return listen(client, "connection-state-change", state => {
setConnected(state === "CONNECTED");
});
} else {
setConnected(false);
}
}, [client]);
return isConnected;
}

export interface NetworkQuality {
/**
* The uplink network quality.
Expand Down

0 comments on commit ee3aa21

Please sign in to comment.