Next.js Discord

Discord Forum

Need help making code use embeds instead of URL's

Answered
bobonlan posted this in #help-forum
Open in Discord
import ImageSlider from "./ImageSlider";
const App = () => {
  const slides = [
    { url: "http://localhost:3000/image-1.jpg", title: "beach" },
    { url: "http://localhost:3000/image-2.jpg", title: "boat" },
    { url: "http://localhost:3000/image-3.jpg", title: "forest" },
    { url: "http://localhost:3000/image-4.jpg", title: "city" },
    { url: "http://localhost:3000/image-5.jpg", title: "italy" },
  ];
  const containerStyles = {
    width: "500px",
    height: "280px",
    margin: "0 auto",
  };
  return (
    <div>
      <h1>Hello monsterlessons</h1>
      <div style={containerStyles}>
        <ImageSlider slides={slides} />
      </div>
    </div>
  );
};

export default App;
Trying to get this code to work using youtube embeds instead of the URL's but cannot figure it out, any insight would be appreciated
Answered by Rafael Almeida
You need to figure out how they generate the thumbnail URL and do the same in your client
View full answer

13 Replies

https://github.com/monsterlessonsacademy/monsterlessonsacademy/blob/221-react-image-slider/src/ImageSlider.js this is the repo for the video i followed along with to build the carousel itself
@bobonlan jsx import ImageSlider from "./ImageSlider"; const App = () => { const slides = [ { url: "http://localhost:3000/image-1.jpg", title: "beach" }, { url: "http://localhost:3000/image-2.jpg", title: "boat" }, { url: "http://localhost:3000/image-3.jpg", title: "forest" }, { url: "http://localhost:3000/image-4.jpg", title: "city" }, { url: "http://localhost:3000/image-5.jpg", title: "italy" }, ]; const containerStyles = { width: "500px", height: "280px", margin: "0 auto", }; return ( <div> <h1>Hello monsterlessons</h1> <div style={containerStyles}> <ImageSlider slides={slides} /> </div> </div> ); }; export default App; Trying to get this code to work using youtube embeds instead of the URL's but cannot figure it out, any insight would be appreciated
Do you mean you want to use youtube URLs instead of JPG image URLs? I don't think this is possible entirely in the client because of CORS and other cross-domain restrictions. For each youtube URL you want to show a thumbnail, you would need to figure out the thumbnail in your server, maybe by visiting the page and finding it in the HTML

As an alternative, you can check how YouTube generate the thumbnail URLs and check if you can do this in a deterministic way in the client, so you can skip the need of a server
Nah, youtube has embeds like this
<iframe width="560" height="315" src="https://www.youtube.com/embed/SK9AlIbexOE?si=L0hLBAu3F8rbTyz_" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
I can get them to render if I make it a react component, but when I paste the react component in instead of
{ url: "http://localhost:3000/image-1.jpg", title: "beach" }
it wont render
Well yeah an embed is not an image, the img tag won't render it
You need to figure out how they generate the thumbnail URL and do the same in your client
Answer
@Rafael Almeida Well yeah an embed is not an image, the `img` tag won't render it
I may just be overlooking it but I don't believe the
img

tag is utilized, afaik every isntance of the word image is used in a context where its just a name and not an actual function
dunno why the box looks like that sorry
The component you shared is showing the images through the background-image property on CSS, which only accept image URLs
If your use case is fine with embeds, you could just add the embed directly in the carousel 🤔
That's what I was hoping to accomplish, I guess I'm going about it the wrong way then :/
I had it that way while using slick react carousel and it worked fine just had a css issue I couldn't figure out, so I opted to make one from scratch and this was the best tutorial I could find but I didn't realize it would only accommodate images
This one probably works with embeds, but you will need to adjust it a bit
Well you've given me a great start to finding the error, gonna go ahead and mark it as solved since you answered the original question. Thank you for the help!