Subchapter 149.10
examples/react-hooks.mdMarkdown10 KBView on GitHub
Official React SDK that provides custom hooks and components for integrating Zoom Video SDK into React apps.
npm install @zoom/videosdk
npm install https://github.com/zoom/videosdk-react/releases/download/v0.0.1/zoom-videosdk-react-0.0.1.tgzPrerequisites:
import {
useSession,
useSessionUsers,
VideoPlayerComponent,
VideoPlayerContainerComponent
} from '@zoom/videosdk-react';
function VideoChat() {
const { isInSession, isLoading, isError } = useSession(
"session123",
"your_jwt_token",
"User Name"
);
const participants = useSessionUsers();
if (isLoading) return <div>Joining session...</div>;
if (isError) return <div>Error joining session</div>;
return (
<div>
{isInSession && (
<VideoPlayerContainerComponent>
{participants.map(participant => (
<VideoPlayerComponent
key={participant.userId}
user={participant}
/>
))}
</VideoPlayerContainerComponent>
)}
</div>
);
}Manages the complete lifecycle of a Zoom video session.
const { isInSession, isLoading, isError, error } = useSession(
topic, // Session topic/ID
token, // JWT authentication token
userName, // Display name
sessionPassword, // Optional session password
sessionIdleTimeoutMins, // Optional idle timeout
{
disableVideo: false,
disableAudio: false,
language: "en-US",
dependentAssets: "Global",
waitBeforeJoining: 0, // Delay before auto-joining
endSessionOnLeave: false, // End session when host leaves
}
);Return values:
| Field | Type | Description |
|---|---|---|
isInSession | boolean | Currently in session |
isLoading | boolean | Session join in progress |
isError | boolean | Error occurred |
error | Error | Error object if any |
Provides real-time access to all session participants with reference stability.
const participants = useSessionUsers();
// participants is an array of Participant objects
participants.map(p => (
<div key={p.userId}>
{p.displayName} - {p.bVideoOn ? 'Video On' : 'Video Off'}
</div>
));Access the local user in the current session.
const myself = useMyself();
return (
<div>
{myself.userName} - {myself.bVideoOn ? 'Video On' : 'Video Off'}
</div>
);Get users who are currently sharing their screen.
const screenshareusers = useScreenShareUsers();
<ScreenShareContainerComponent>
{screenshareusers.map(userId => (
<ScreenSharePlayerComponent key={userId} userId={userId} />
))}
</ScreenShareContainerComponent>Manages video capture state and controls.
const { isVideoOn, toggleVideo, setVideo } = useVideoState();
// Toggle video on/off
<button onClick={() => toggleVideo({ fps: 30 })}>
{isVideoOn ? 'Turn Off Video' : 'Turn On Video'}
</button>
// Set video state explicitly
<button onClick={() => setVideo(true, { fps: 15 })}>
Enable Video
</button>Comprehensive audio state management.
const {
isAudioMuted,
isCapturingAudio,
toggleMute,
toggleCapture,
setMute,
setCapture
} = useAudioState();
// Toggle mute
<button onClick={toggleMute}>
{isAudioMuted ? 'Unmute' : 'Mute'}
</button>
// Toggle audio capture
<button onClick={toggleCapture}>
{isCapturingAudio ? 'Stop Audio' : 'Start Audio'}
</button>Manages screen sharing functionality.
const { ScreenshareRef, startScreenshare } = useScreenshare();
return (
<div>
<LocalScreenShareComponent ref={ScreenshareRef} />
<button onClick={() => startScreenshare({ audio: true })}>
Start Screen Share
</button>
</div>
);Required container for video players. Must wrap all VideoPlayerComponent instances.
<VideoPlayerContainerComponent style={{ width: '100%', height: '400px' }}>
{participants.map(participant => (
<VideoPlayerComponent key={participant.userId} user={participant} />
))}
</VideoPlayerContainerComponent>Renders individual participant video streams.
const participants = useSessionUsers();
<VideoPlayerComponent user={participants[0]} />Required container for screen share players.
<ScreenShareContainerComponent style={{ width: '100%', height: '400px' }}>
{screenshareusers.map(userId => (
<ScreenSharePlayerComponent key={userId} userId={userId} />
))}
</ScreenShareContainerComponent>Renders screen share streams.
<ScreenSharePlayerComponent userId={screenshareusers[0]} />import React from 'react';
import {
useSession,
useSessionUsers,
useMyself,
useVideoState,
useAudioState,
useScreenshare,
useScreenShareUsers,
VideoPlayerComponent,
VideoPlayerContainerComponent,
ScreenSharePlayerComponent,
ScreenShareContainerComponent,
LocalScreenShareComponent
} from '@zoom/videosdk-react';
interface VideoCallProps {
topic: string;
token
The React SDK is designed to work alongside the core @zoom/videosdk. You can use both:
import ZoomVideo from '@zoom/videosdk';
import { useSession, useSessionUsers } from '@zoom/videosdk-react';
// Use React hooks for common patterns
const { isInSession } = useSession(topic, token, userName);
const participants = useSessionUsers();
// Access the underlying client for advanced features
const client = ZoomVideo.createClient();
const chatClient = client.getChatClient();
const recordingClient = client.getRecordingClient();src/
├── components/ # React components
│ ├── VideoPlayerComponent
│ ├── VideoPlayerContainerComponent
│ ├── ScreenSharePlayerComponent
│ ├── ScreenShareContainerComponent
│ └── LocalScreenShareComponent
├── hooks/ # Custom React hooks
│ ├── useSession
│ ├── useSessionUsers
│ ├── useMyself
│ ├── useVideoState
│ ├── useAudioState
│ ├── useScreenshare
│ └── useScreenShareUsers
└── index.ts # Main exports| Benefit | Description |
|---|---|
| Simplified State | Automatic participant state management |
| Reference Stability | Hooks maintain stable references |
| TypeScript Support | Full type definitions included |
| Flexible | Use alongside core SDK |
| Customizable | Components accept standard React props |