React components are self-contained units of UI logic that encapsulate markup, state, and behavior. They define isolated UI logic using JavaScript functions or classes. JSX defines component structure using a declarative syntax that compiles to `React.createElement` calls. React maintains a virtual DOM to track state changes and compute minimal updates. In video applications, components manage player interfaces, control logic, playback state, and interactive overlays.

Types of React Components

React defines components as either functions or classes. Both types receive props and return UI elements. Functional components use hooks to manage state and side effects. Class components extend a base class and rely on lifecycle methods. Each type executes within the same rendering model but handles logic differently.

Functional Components

Functional components use plain JavaScript functions to return JSX. React runs these functions on each render. Hooks such as useState manage internal state without class syntax. Functional components recompute output when state or props change.

Example:

code
import { useState } from 'react';
code

code
function VideoPlayer({ src }) {
code
const [playing, setPlaying] = useState(false);
code

code
const togglePlayback = () => {
code
setPlaying(prev => !prev);
code
};
code

code
return (
code
<div>
code
<video src={src} controls autoPlay={playing} />
code
<button onClick={togglePlayback}>
code
{playing ? 'Pause' : 'Play'}
code
</button>
code
</div>
code
);
code
}
code

Explanation:

  • Initializes a playing state variable to false (video is paused by default).
  • setPlaying is the corresponding state updater function.
  • useState(false) ensures the state persists across renders and triggers UI updates when modified.

Class Components

Class components inherit from React.Component and implement a render method to return JSX. They initialize the state in the constructor and update it. Arrow functions bind methods like togglePlayback to the class instance. React calls render on each state or prop change to update the UI.s

Example:

code
import React from 'react';
code

code
class VideoPlayer extends React.Component {
code
constructor(props) {
code
super(props);
code
this.state = { playing: false };
code
}
code

code
togglePlayback = () => {
code
this.setState(prevState => ({
code
playing: !prevState.playing
code
}));
code
};
code

code
render() {
code
const { src } = this.props;
code
const { playing } = this.state;
code

code
return (
code
<div>
code
<video src={src} controls autoPlay={playing} />
code
<button onClick={this.togglePlayback}>
code
{playing ? 'Pause' : 'Play'}
code
</button>
code
</div>
code
);
code
}
code
}
code

Explanation:

  • constructor(props) initializes the component and receives props.
  • super(props) passes props to the parent React.Component constructor.
  • this.state = { playing: false } sets the initial internal state; video is paused by default.

Functional vs. Class Components

FeatureFunctional ComponentClass Component
State ManagementuseState hookthis.state and setState
Side EffectsuseEffect hookcomponentDidMount/Update
SyntaxArrow/functionsES6 class with Component

How JSX Compiles and Renders DOM Efficiently

JSX defines UI using HTML-like syntax within JavaScript. React compiles JSX into React.createElement calls. JSX supports embedding expressions, attributes, and nested structures. React parses JSX during build time and converts it into a virtual DOM structure. This structure combines component logic and layout in a single unit.

JSX Structure and Rules

JSX uses standard JavaScript syntax rules. Void elements require self-closing tags, such as <video/>. Curly braces embed JavaScript expressions within markup. Attributes use camelCase, with className replacing class. Boolean attributes must be assigned explicitly, such as controls={true}. JSX also prohibits assigning multiple root elements without a wrapper like <div> or <>.

Example:

code
function Greeting({ user }) {
code
return <h1 className="header">Hello, {user.name}!</h1>;
code
}

Explanation:

  • className: Maps to class.
  • {user.name}: Embeds JS expression.

Video Workflow with React

React simplifies complex video UIs through composability and declarative syntax. React uses a component-based model to define UI. Each component contains its structure, logic, and state. The virtual DOM computes changes and applies minimal updates to the real DOM.

React updates components based on state or prop changes. Components can nest and compose into larger UI structures. Context and hooks provide shared state and side-effect management.

Custom Video Player Component

Custom components in React manage playback by combining state and references. The useState hook tracks playback status, while useRef accesses the video element for control. Logic inside event handlers updates both the UI and playback behavior. This setup connects user interaction with media state through React’s controlled component structure.

Example:

code
import { useState, useRef } from 'react';
code

code
function CustomPlayer({ url }) {
code
const [isPlaying, setIsPlaying] = useState(false);
code
const videoRef = useRef(null);
code

code
const togglePlay = () => {
code
if (isPlaying) videoRef.current.pause();
code
else videoRef.current.play();
code
setIsPlaying(!isPlaying);
code
};
code

code
return (
code
<div>
code
<video ref={videoRef} src={url} />
code
<button onClick={togglePlay}>
code
{isPlaying ? 'Pause' : 'Play'}
code
</button>
code
</div>
code
);
code
}

Explanation:

  • useRef: Points to <video> node.
  • togglePlay: Pauses or plays video.

Rendering Video Metadata

React renders metadata by computing values inside components. Functions like formatTime convert raw data before output. JSX uses these values based on props without changing them. This keeps formatting and layout logic within a single component.

Example:

code
function VideoInfo({ duration, views }) {
code
const formatTime = (seconds) =>
code
new Date(seconds * 1000).toISOString().substr(11, 8);
code

code
return (
code
<div>
code
<span>Duration: {formatTime(duration)}</span>
code
<span>Views: {views.toLocaleString()}</span>
code
</div>
code
);
code
}

Explanation:

  • formatTime: Formats seconds to HH:MM:SS.
  • toLocaleString(): Adds commas.

Integrating Third-Party Video Libraries

React loads third-party video libraries as components. It passes configuration through props to components like react-player. JSX places these components in the render tree. React triggers updates by changing props or re-rendering the parent component.

Example:

code
import ReactPlayer from 'react-player';
code

code
function StreamPlayer() {
code
return (
code
<ReactPlayer
code
url="twitch.tv/stream"
code
controls
code
width="100%"
code
height="auto"
code
/>
code
);
code
}

Explanation:

  • controls: Built-in UI.
  • width/height: Responsive props.

Handling Playback State

React handles playback state by connecting components to a global store using Redux. useSelector reads the current playback state from the store. useDispatch sends actions to update the state. React re-renders the component when the store updates, keeping the UI in sync with playback status.

Example:

code
import { useSelector, useDispatch } from 'react-redux';
code
import { play, pause } from './videoSlice';
code

code
function ReduxPlayer() {
code
const isPlaying = useSelector(state => state.video.isPlaying);
code
const dispatch = useDispatch();
code

code
return (
code
<button onClick={() => isPlaying ? dispatch(pause()) : dispatch(play())}>
code
{isPlaying ? 'Pause' : 'Play'}
code
</button>
code
);
code
}

Explanation:

  • useSelector: Reads from store.
  • dispatch: Sends actions.