Function useJoin

  • This hook lets a user automatically join a channel when the component is ready and automatically leaves the channel when the component is unmounted. You can customize the conditions required to join a channel using fetchArgs. For example, generating a token and other asynchronous operations can be performed before joining the channel.

    Parameters

    • fetchArgs: FetchArgs

      The parameters or asynchronous function required to join the channel. See JoinOptions for details.

    • ready: boolean = true

      Whether the user is ready to join the channel. The default value is true.

    • Optional client: null | IAgoraRTCClient

      Created using the Web SDK's IAgoraRTC.createClient method.

    Returns {
        data: UID;
        error: AgoraRTCReactError | null;
        isConnected: boolean;
        isLoading: boolean;
    }

    Example

    import { useJoin } from "agora-rtc-react";

    function App() {
    // Example: passing a function as first argument
    // useJoin(async () => {
    // Fetch the token before joining the channel. Note that the data type of getData must be fetchArgs
    // const getData = await getToken();
    // return getData;
    // }, calling);

    useJoin(
    {
    appid: YOUR_APPID,
    channel: YOUR_CHANNEL,
    token: YOUR_TOKEN,
    },
    ready,
    );

    return <></>;
    }