Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How to create a table in ReactJS ?
Next article icon

Create a Searchable Video List in React

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In ReactJS, we can embed the video previews as an important feature in the application. There are multiple videos with the exact similar names. To filter out the videos or to search the specific term video in the application, we can create a search video list.

In this article, we will see how we can create this searchable video list with two unique approaches with practical implementation in terms of examples.

Table of Content

  • Using State and Filtering
  • Using useRef and useEffect

Steps to Create the React Application

Step 1: Create a React application using the following command.

npx create-react-app foldername

Step 2: Once done, change your directory to the newly created application using the following command.

cd foldername

Project Structure:

The updated dependencies in package.json for frontend will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Approach 1: Using State and Filtering:

In this approach, we are using state management to filter the videos based on the dynamically updated search term. According to the search term, the dynamic filtering of videos is done and the filtered videos are shown to the user.

Example: Below is the implementation of above discussed approach.

CSS
body {     font-family: 'Arial', sans-serif;     margin: 0;     padding: 0;     background-color: #f4f4f4; } header {     color: green;     padding: 1rem;     font-weight: bold;     text-align: center; }  main {     display: flex;     justify-content: center;     align-items: center;     flex-direction: column;     padding: 1rem; }  input[type="text"] {     width: 80%;     padding: 0.5rem;     margin-bottom: 1rem;     border: 1px solid #ddd;     border-radius: 4px; }  ul {     list-style-type: none;     padding: 0;     display: flex;     overflow-x: auto; }  li {     margin-right: 1rem;     border-radius: 8px;     box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);     transition: transform 0.3s ease-in-out; }  li:hover {     transform: scale(1.05); }  a {     text-decoration: none;     color: #333; }  h3 {     margin: 0;     padding: 1rem;     text-align: center; }  img {     width: 100%;     height: 100%;     object-fit: cover;     border-radius: 8px 8px 0 0; } 
JavaScript
// App.js import React, { useState } from 'react'; import './App.css'; const VideoList = ({ videos, searchTerm, handleSearch }) => { 	const filterVideo = videos.filter(video => 		video.title.toLowerCase() 			.includes(searchTerm.toLowerCase()) 	); 	return ( 		<div> 			<input 				type="text" 				placeholder="Search videos Here" 				value={searchTerm} 				onChange={e => handleSearch(e.target.value)} 			/> 			<ul> 				{filterVideo.map(video => ( 					<li key={video.id}> 						<div> 							<a href={video.link} target="_blank" 								rel="noopener noreferrer"> 								<h3>{video.title}</h3> 								<img 									src= {`https://img.youtube.com/vi/${video.link.split('v=')[1]}/0.jpg`} 									alt="Video Preview" 								/> 							</a> 						</div> 					</li> 				))} 			</ul> 		</div> 	); }; const App = () => { 	const [search_Term, set_Search_Term] = useState(''); 	const [videos, setVideos] = useState([ 		{ 			id: 1, 			title: 'Top 10 Languages', 			link:  			'https://www.youtube.com/watch?v=ehXa1Hxxu3Y', 		}, 		{ 			id: 2, 			title: 'Something BIG', 			link: 'https://www.youtube.com/watch?v=lbveOrbHgto', 		}, 		{ 			id: 3, 			title: 'Microsoft Journey', 			link: 'https://www.youtube.com/watch?v=0mRyTDhxe_s', 		}, 		{ 			id: 4, 			title: 'Web Security', 			link: 'https://www.youtube.com/watch?v=578Pzg7tVF4', 		}, 		{ 			id: 5, 			title: 'Extract Audio', 			link: 'https://www.youtube.com/watch?v=SrPi0FrmGiE', 		}, 		{ 			id: 6, 			title: 'Graphs DS', 			link: 'https://www.youtube.com/watch?v=pCmsQVHYXK0', 		}, 	]);  	const searchFn = query => { 		set_Search_Term(query); 	};  	return ( 		<div> 			<header> 				<h1>GeeksforGeeks</h1> 			</header> 			<h3>Approach 1: Using State and Filtering</h3> 			<main> 				<VideoList videos={videos}  					searchTerm={search_Term}  					handleSearch={searchFn} /> 			</main> 		</div> 	); }; export default App; 

Step to run the application: Open the terminal and type the following command.

npm start

Output: Open the browser and our project is shown in the URL http://localhost:3000/

Approach 2: Using useRef and useEffect

In this approach, we are using the useRef to create the reference of each search input which allows direct access to its current value. Then the useEffect hook is mainly used to attach an event listener to the input which triggers a search function on each input change by filtering and showing the filtered video ist.

Example: Below is the implementation of above discussed approach.

JavaScript
import React, { useState, useEffect, useRef } from 'react'; const App = () => { 	const videoData = [ 		{ 			id: 1, 			title: 'Top 10 Languages', 			link: 'https://www.youtube.com/watch?v=ehXa1Hxxu3Y', 			preview: 'https://img.youtube.com/vi/ehXa1Hxxu3Y/maxresdefault.jpg', 		}, 		{ 			id: 2, 			title: 'Something BIG', 			link: 'https://www.youtube.com/watch?v=lbveOrbHgto', 			preview: 'https://img.youtube.com/vi/lbveOrbHgto/maxresdefault.jpg', 		}, 		{ 			id: 3, 			title: 'Microsoft Journey', 			link: 'https://www.youtube.com/watch?v=0mRyTDhxe_s', 			preview: 'https://img.youtube.com/vi/0mRyTDhxe_s/maxresdefault.jpg', 		}, 		{ 			id: 4, 			title: 'Web Security', 			link: 'https://www.youtube.com/watch?v=578Pzg7tVF4', 			preview: 'https://img.youtube.com/vi/578Pzg7tVF4/maxresdefault.jpg', 		}, 		{ 			id: 5, 			title: 'Extract Audio', 			link: 'https://www.youtube.com/watch?v=SrPi0FrmGiE', 			preview: 'https://img.youtube.com/vi/SrPi0FrmGiE/maxresdefault.jpg', 		}, 		{ 			id: 6, 			title: 'Graphs DS', 			link: 'https://www.youtube.com/watch?v=pCmsQVHYXK0', 			preview: 'https://img.youtube.com/vi/pCmsQVHYXK0/maxresdefault.jpg', 		}, 	]; 	const [videos, setVideos] = useState(videoData); 	const inputRef = useRef(); 	useEffect(() => { 		const searchFn = () => { 			const searchVar = inputRef.current.value.toLowerCase(); 			const filteredVideo = videoData.filter((video) => 				video.title.toLowerCase().includes(searchVar) 			); 			setVideos(filteredVideo); 		}; 		inputRef.current.addEventListener('input', searchFn); 		return () => { 			inputRef.current.removeEventListener('input', searchFn); 		}; 	}, []);  	return ( 		<div> 			<h1 style={ 				{ 					color: 'green', 					textAlign: "center" 				} 			}> 				GeeksforGeeks 			</h1> 			<h3 style={{ margin: "5px 5px" }}> 				Approach 2: Using useRef and useEffect 			</h3> 			<input 				type="text" 				placeholder="Search videos Here" 				ref={inputRef} 				style={ 					{ 						width: '80%', marginLeft: "10px", 						padding: '10px', fontSize: '16px', 						border: "2px solid black" 					}} 			/> 			<ul style={ 				{ 					listStyle: 'none', padding: 0, 					display: 'flex', flexWrap: 'wrap' 				}}> 				{videos.map((video) => ( 					<li key={video.id} 						style={ 							{ 								margin: '10px', 								flex: '0 0 30%', 								minWidth: '200px' 							}}> 						<a href={video.link} 							target="_blank" 							rel="noopener noreferrer"> 							<img 								src={video.preview} 								alt={video.title} 								style={{ width: '80%', height: 'auto' }}/> 							<p>{video.title}</p> 						</a> 					</li> 				))} 			</ul> 		</div> 	); }; export default App; 

Step to run the application: Open the terminal and type the following command.

npm start

Output: Open the browser and our project is shown in the URL http://localhost:3000/


Next Article
How to create a table in ReactJS ?

G

gauravggeeksforgeeks
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • Geeks Premier League 2023

Similar Reads

  • Create a Video Editor using React
    Video Editor is one of the useful apps in day-to-day life. In this article, we’ll walk you through the process of building a basic video editing app using React Native. The application enables users to upload, trim, and convert specific scenes to GIFs and then download the final edited file directly
    6 min read
  • How to create a table in ReactJS ?
    In ReactJS, tables are commonly used to display data in rows and columns. Tables can be static, where data is hardcoded, or dynamic, where data is passed from an array or fetched from an API. React makes it simple to create interactive and dynamic tables, with additional features such as sorting, pa
    6 min read
  • How to Create List Styling in ReactJS ?
    React, a declarative, efficient, and flexible JavaScript library for building user interfaces, plays a crucial role as the 'V' in MVC. Specifically, ReactJS, an open-source, component-based front-end library maintained by Facebook, focuses on the view layer of applications. In this article, we will
    3 min read
  • Create a Video Streaming Platform with React
    This article focuses on Creating and Designing a Video Streaming Application using React JS. In this application, users can stream any video just by entering the video link in the input field, the preview and streaming of the video is started in the application itself. Proper error messages are show
    4 min read
  • How to create Video Player in ReactJS ?
    We are going to learn how we can create a Video Player in React JS. A video player is a kind of media player for playing back digital video data. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. Prerequisite:React JSPhases of JavaScript Even
    2 min read
  • How to create dynamic search box in ReactJS?
    The dynamic search box is a search bar with a text field to take the user input and then perform some operation on the user input to show him the dynamic results based on his input. API calls are made whenever a user starts typing in order to show him the dynamic options. For example Youtube search
    2 min read
  • Create a Video Player App using React-Native
    React-Native is an open-source JavaScript framework used to broaden cross-platform packages i.e., you may write code in React-Native and publish it as an Android or IOS app. In this article, we will build a Video Player app with the usage of React-Native. The app will run on both Android and IOS. Pr
    4 min read
  • Create a Video Streaming App using React-Native
    React-Native is an open-source JavaScript framework used to broaden cross-platform packages i.e., you may write code in React-Native and publish it as an Android or IOS app. In this article, we will build a Video Streaming app using React-Native. The app will run on both Android and IOS. Preview of
    4 min read
  • Create a Snake Game in React
    Snake Game using ReactJS project implements functional components and manages the state accordingly. The developed Game allows users to control a snake using arrow keys or touch the buttons displayed on the screen to collect food and grow in length. The goal of the game is to eat as much food as pos
    7 min read
  • Drag and Drop Sortable List Using ReactJS
    Building a drag and drop sortable list in ReactJS allows users to rearrange items interactively by dragging and dropping. This feature enhances the user experience by providing dynamic way to manage lists or items. In this article, we’ll explore how to implement a drag and drop sortable list in Reac
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences