-
Notifications
You must be signed in to change notification settings - Fork 9
/
useRTCClient.tsx
44 lines (38 loc) · 1.4 KB
/
useRTCClient.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
39
40
41
42
43
44
import type { IAgoraRTCClient } from "agora-rtc-sdk-ng";
import { createContext, useContext } from "react";
import type { AgoraRTCProviderProps } from "./types";
const AgoraRTCContext = /* @__PURE__ */ createContext<IAgoraRTCClient | null>(null);
export function AgoraRTCProvider({ client, children }: AgoraRTCProviderProps) {
return <AgoraRTCContext.Provider value={client}>{children}</AgoraRTCContext.Provider>;
}
/**
* @ignore
*/
function useOptionalRTCClient(client?: IAgoraRTCClient | null): IAgoraRTCClient | null {
const clientFromContext = useContext(AgoraRTCContext);
return client || clientFromContext;
}
/**
* Returns the IAgoraRTCClient object.
*
* @param client - If provided, the passed `IAgoraRTCClient` object is returned. If not provided, the `IAgoraRTCClient` object obtained from the [parent component's context](https://api-ref.agora.io/en/video-sdk/reactjs/2.x/functions/AgoraRTCProvider.html) is returned.
* @example
* ```jsx
* import { useRTCClient } from "agora-rtc-react";
*
* function App() {
* const client = useRTCClient();
*
* return <></>;
* }
* ```
*/
export function useRTCClient(client?: IAgoraRTCClient | null): IAgoraRTCClient {
const resolvedClient = useOptionalRTCClient(client);
if (!resolvedClient) {
throw new Error(
"Agora RTC client not found. Should be wrapped in <AgoraRTCProvider value={client} />",
);
}
return resolvedClient;
}