DEV Community

Cover image for Simplest way to embed a Youtube video in your React app

Simplest way to embed a Youtube video in your React app

bravemaster619 on December 17, 2020

Overview Quick and simple No npm i Fully responsive Feel free to Copy & Paste 1. Make a component for iframe: Youtube...
Collapse
 
prmichaelsen profile image
Patrick Michaelsen • Edited

one change I needed: export const YoutubeEmbed = (embedId) =>
embedId && ( <iframe ... />);

jk that didn't fix my problem. for some reason it only works when I hard code the embedId

It's trying to fetch embed/[Object object]

For completenes sake, turns out it's a problem with my code that generates the embedId. I wish you could edit comments so I didn't have to triple post, but alas.

Collapse
 
eyassh profile image
Eyas

@prmichaelsen That's because you have a typo, instead of:

export const YoutubeEmbed = (embedId) =>

try:

export const YoutubeEmbed = ({embedId}) =>

The [Object object] in your string is the first argument. You call it embedId here, but the first argument to a React FunctionComponent is the props object. For this case, it will look something like this:

{
  embedId: "myidstring",
  children: null
}
Enter fullscreen mode Exit fullscreen mode

or similar.

Collapse
 
juansystems profile image
JuanSystems

Funciona bien. Pero me sale este error 404 en la consola de chrome:

GET youtube.com/img/meh7.png 404 index.js:1962

Image description

Collapse
 
abdallahabusedo profile image
abdallah abu sedo

wow great and fast way ,
can you explain to me how to fetch the embedId from and playlist videos

Collapse
 
britt_joiner profile image
Brittany Joiner

This was awesome! Worked really well for me as well!

Collapse
 
tomshaw profile image
Tom Shaw

Nice!

Collapse
 
nivethan profile image
Nivethan Ariyaratnam • Edited

the example video shows a thumbnail as expected but when i try with 2 videos they don't show any thumbnail but just a black screen! do you have any idea?

Collapse
 
yashkapure06 profile image
Yash Kapure

I am trying the same since hours but I am not getting the results.
As well as there is no error in console_

Collapse
 
dpagey profile image
Pagey

Awesome, exactly what I needed, thank you!

Collapse
 
boyld222 profile image
Tri Kiet

Really really need it, thank you very much !

Collapse
 
hhsm95 profile image
Hugo Sandoval

Thanks, works like a charm!

Collapse
 
mdaralola profile image
{{ Micheal Daralola }} • Edited

How did you get the EMBED id?

Collapse
 
nivethan profile image
Nivethan Ariyaratnam

you can find it last on the url

Collapse
 
iqbalbary profile image
Iqbal Bary Shuvo

Excellent

Collapse
 
transiient profile image
Transient • Edited

As simple as it could be, thanks!

Collapse
 
jacobdchamberlain profile image
Jacob D. Chamberlain

This was super helpful, and straight to the point. Thank you so much!

Collapse
 
jjrh92 profile image
Julio

Thank you.

Collapse
 
danielngan17 profile image
Daniel Ngan

can you write it in functional components?

Collapse
 
athul_jose_bb907dcdcb66e8 profile image
Athul Jose

can we hide the info of the video and the links that redirect to youtube