Movie Trailer app using ReactJS
Last Updated :
29 Jul, 2024
In this article, we are going to make a simple app that searches for any movie/web series trailers. The users can search for their favourite movie trailers using this application. The API will fetch the trailer from the internet and display it on the webpage. We will use 'movie-trailer' npm package to find such URLs and display the content using another npm package called 'react-player'.Â
Let us have a look at how the final application will look like:
.jpg)
Prerequisites: The pre-requisites for this project are:
Approach: Our app contains two sections i.e a section for taking the user input and the other for displaying the video. Whenever a user searches for a video, we will store that inside a state variable and whenever a user clicks on the search button we will call a function that will fetch the required video URL and store it in another state variable. Now we have the required URL, we will simply render that video using the 'ReactPlayer' component.
Creating React app and installing module:Â
- Step 1: Create a react application by typing the following command in the terminal:
npx create-react-app movie-app
- Step 2: Now, go to the project folder i.e movie-app by running the following command:
cd movie-app
npm install movie-trailer
npm install react-player
Project Structure: It should look like this:

The updated dependencies in package.json will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"movie-trailer": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-player": "^2.12.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example code: Write the following code in respective files.
- App.js file: This file contains our app logic:
- App.css file:This file contains all the required styling for that app:
CSS
/* App.css */
.App {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
height: 100vh;
width: 100%;
font-size: 22px;
}
.search-box {
height: 10vh;
}
.search-box>input,
button {
box-sizing: border-box;
height: 25px;
font-size: 20px;
}
JavaScript
// App..js
import './App.css';
import { useState } from 'react';
import ReactPlayer from 'react-player';
import movieTrailer from 'movie-trailer';
function App() {
//Setting up the initial states using
// react hook 'useState"
const [video, setVideo] = useState("inception");
const [videoURL, setVideoURL] =
useState("https://youtu.be/sa9l-dTv9Gk");
//A function to fetch the required URL
// and storing it inside the
// videoURL state variable
function handleSearch() {
movieTrailer(video).then((res) => {
setVideoURL(res);
});
}
return (
<div className="App">
<div className="search-box">
<label>
Search for any movies/shows:{" "}
</label>
<input type="text" onChange=
{(e) => { setVideo(e.target.value) }} />
<button onClick={() => { handleSearch() }}>
Search
</button>
</div>
// Using 'ReactPlayer' component to
// display the video
<ReactPlayer url={videoURL} controls={true} />
</div>
);
}
export default App;
Step to run application: Type the following command in the terminal:
npm start
Output: Now Open http://localhost:3000/ Â in your browser to see our working app.
Similar Reads
BMI Calculator Using React In this article, we will create a BMI Calculator application using the ReactJS framework. A BMI calculator determines the relationship between a person's height and weight. It provides a numerical value that categorizes the individual as underweight, normal weight, overweight, or obese.Output Previe
3 min read
Create Rock Paper Scissor Game using ReactJS In this article, we will create Rock, Paper, Scissors game using ReactJS. This project basically implements class components and manages the state accordingly. The player uses a particular option from Rock, Paper, or Scissors and then Computer chooses an option randomly. The logic of scoring and win
6 min read
Create a Form using React JS Creating a From in React includes the use of JSX elements to build interactive interfaces for user inputs. We will be using HTML elements to create different input fields and functional component with useState to manage states and handle inputs. Prerequisites:Functional ComponentsJavaScript ES6JSXPr
5 min read
Create a Random Joke using React app through API In this tutorial, we'll make a website that fetches data (joke) from an external API and displays it on the screen. We'll be using React completely to base this website. Each time we reload the page and click the button, a new joke fetched and rendered on the screen by React. As we are using React f
3 min read
Nutrition Meter - Calories Tracker App using React GeeksforGeeks Nutrition Meter application allows users to input the name of a food item or dish they have consumed, along with details on proteins, calories, fat, carbs, etc. Users can then keep track of their calorie intake and receive a warning message if their calorie limit is exceeded. The logic
9 min read
Currency converter app using ReactJS In this article, we will be building a very simple currency converter app with the help of an API. Our app contains three sections, one for taking the user input and storing it inside a state variable, a menu where users can change the units of conversion, and finally, a display section where we dis
4 min read
Lap Memory Stopwatch using React Stopwatch is an application which helps to track time in hours, minutes, seconds, and milliseconds. This application implements all the basic operations of a stopwatch such as start, pause and reset button. It has an additional feature using which we can keep a record of laps which is useful when we
5 min read
Typing Speed Tester using React In this article, we will create a Typing Speed Tester that provides a random paragraph for the user to type as accurately and quickly as possible within a fixed time limit of one minute. This application also displays the time remaining, counts mistakes calculates the words per minute and characters
9 min read
Number Format Converter using React In this article, we will create Number Format Converter, that provides various features for users like to conversion between decimal, binary, octal and hexadecimal representations. Using functional components and state management, this program enables users to input a number and perform a range of c
7 min read
Create a Password Validator using ReactJS Password must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in ReactJS. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions a
2 min read