Video src not updating with state change
Answered
Gharial posted this in #help-forum
GharialOP
I'm building a video card, that when you click it, a modal pops up with a video in it. Each card is rendered from it's own object in an object array, so each has it's unique video src link. When I click a card, the current video object from the context should update with the object of the selected card, so the modal contains a new video.
Unfortunately, the modal only renders the initial video even after you click a new card and the src link updates. I'm not sure why this is happening.
Here's the code for the modal:
Unfortunately, the modal only renders the initial video even after you click a new card and the src link updates. I'm not sure why this is happening.
Here's the code for the modal:
"use client";
import {
ModalContext,
ModalContextInterface,
initialVid,
} from "@/context/ModalContext";
import { useContext, useEffect, useState } from "react";
const VideoModal = () => {
const { video, setVideo }: ModalContextInterface = useContext(ModalContext);
const [open, setOpen] = useState(false);
useEffect(() => {
if (video !== initialVid) {
setOpen(true);
} else {
setOpen(false);
}
}, [video]);
return (
<div
className={`${
open ? "opacity-100 block" : "opacity-0 hidden"
} z-30 p-6 sm:p-8 justify-center items-center fixed top-0 bottom-0 right-0 left-0 mx-auto my-auto`}
>
<video
controls
autoPlay
loop
playsInline
muted
width={1080}
height={1080}
className={`h-full w-full rounded-lg bg-contain bg-center object-cover`}
>
<source src={video?.video} type="video/mp4" />
</video>
</div>
);
};Answered by Gharial
Here's a solution for anyone interested: https://stackoverflow.com/questions/41303012/updating-source-url-on-html5-video-with-react
3 Replies
GharialOP
This is the code for the video card:
<div className="grid w-[100%] grid-cols-2 grid-rows-1 place-content-center place-items-center gap-x-4 gap-y-4 sm:gap-x-[30px] sm:gap-y-6 lg:gap-x-10 lg:gap-y-8 sm:grid-cols-3 lg:grid-cols-4">
{filteredVideos.map((video, index) => {
return (
<RegularCard
key={index}
title={video.title}
thumbnail={video.thumbnail}
video={video.video}
year={video.year}
category={video.category}
rating={video.rating}
isBookmarked={video.isBookmarked}
isTrending={video.isTrending}
onClick={() => setVideo(video)}
/>
);
})}
</div>GharialOP
Here's a solution for anyone interested: https://stackoverflow.com/questions/41303012/updating-source-url-on-html5-video-with-react
Answer
@Gharial Here's a solution for anyone interested: https://stackoverflow.com/questions/41303012/updating-source-url-on-html5-video-with-react
mark this message as answered if you think this is answered ðŸ‘