Embed YouTube video in ReactJS
Today we’ll show you how to embed YouTube video in ReactJS application. In this article, we’ll use the react-player npm package to embed the YouTube video. You can also embed video of other sites like Vimeo, Dailymotion, etc.
react youtube player, reactjs player full screen, react video thumbnail, react player responsive, React JS embed video of YouTube, SoundCloud, Facebook, Vimeo, Twitch, Streamable, Wistia, DailyMotion, Mixcloud, Files like mp4, ogv, webm, mp3, Multiple, HLS (m3u8), DASH (mpd), Create Youtube Player in ReactJs example component, Full width Youtube embed with React.js.
Checkout more articles on ReactJS
Steps to embed YouTube video in React
1. Create react application
Let’s create a sample react application using the create-react-app npm package. If you don’t know how to create an application then refer to the link of Create React Application.
2. Install npm package dependency
As we mentioned, we’ll be using the react-player npm package to embed a YouTube video. So let’s get ReactPlayer installed to our application.
1 | npm i react-player |
3. Create component to embed YouTube video
Here, we are going to update the App.js
file to embed video. Your code should look like this.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import React from 'react'; import ReactPlayer from "react-player"; function App() { return ( <div className="App"> <h3>Embed YouTube video - <a href="https://www.cluemediator.com">Clue Mediator</a></h3> <ReactPlayer url="https://www.youtube.com/watch?v=UVCP4bKy9Iw" /> </div> ); } export default App; |
There are multiple options available to play with the video player like playing, loop, controls, volume, etc.
If you are only working with single player then it’s good to import individual player to reduce the bundle size of the application.
1 2 3 4 5 6 | import YouTubePlayer from “react-player/lib/players/YouTube”; ... ... <YouTubePlayer url='https://www.youtube.com/watch?v=d46Azg3Pm4c' /> |
4. Output
Run the project and check the output.
That’s it for today.
Thank you for reading. Happy Coding!
thank you <3