Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

React.js Project to Embed Youtube Video in IFrame inside Browser Without any Library in Javascript

Posted on February 12, 2023

 

 

Welcome folks today in this blog post we will be embeding youtube video in iframe tag using react.js. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js project using the below command as shown below

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And now you need to edit the App.js file of your react.js app and copy paste the below code

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
import React from "react";
import "./styles.css";
import YoutubeEmbed from "./YoutubeEmbed";
 
export default function App() {
  return (
    <div className="App">
      <h1>React Youtube Embedding Example</h1>
      <YoutubeEmbed embedId="rokGy0huYEA" />
    </div>
  );
}

 

 

As you can see in the above javascript code we are including the youtubeEmbed component where we are passing the embedId to this component so that we can display the youtube video in iframe tag.

 

Now we need to create the YoutubeEmbed.js file and copy paste the following code

 

 

YoutubeEmbed.js

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import React from "react";
import PropTypes from "prop-types";
 
const YoutubeEmbed = ({ embedId }) => (
  <div className="video-responsive">
    <iframe
      width="853"
      height="480"
      src={`https://www.youtube.com/embed/${embedId}`}
      frameBorder="0"
      allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
      allowFullScreen
      title="Embedded youtube"
    />
  </div>
);
 
YoutubeEmbed.propTypes = {
  embedId: PropTypes.string.isRequired
};
 
export default YoutubeEmbed;

 

 

As you can see inside this we have the iframe tag in which we are providing the width and the height attributes and then we have the src of the video where we are attaching the embedId of the video the url. And then we are attaching the frameborder and also we are adding the allowfullscreen attribute as well.

 

Now if you open the application inside the browser you will see the below result

 

 

 

Recent Posts

  • Angular 14/15 JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme