I´m using react-player in nextjs-app to show videos fetched via youtube-api. Everything works fine, modal gets open after clicking on the playBtn but then the player plays all videos, not just the selected one. I really don´t know why because I set the videoId into the url of RectPlayer dynamically. Would somebody tell me please what I did wrong? Here is the source:
export default function Playlist({ videos }) {
const [open, setOpen] = useState(false);
const onOpenModal = () => setOpen(true);
const onCloseModal = () => setOpen(false);
const videoURL = "https://www.youtube.com/watch?v=";
const sortedVids = videos
.sort((a, b) =>
Number(
new Date(b.contentDetails.videoPublishedAt) -
Number(new Date(a.contentDetails.videoPublishedAt))
)
)
return (
...
{sortedVids.map((vid, id) => {
return (
<div className={styles.item_container} key={id}>
<div className={styles.clip_container}>
<Image
className={styles.thumbnails}
src={vid.snippet.thumbnails.medium.url}
layout="fill"
objectFit="cover"
alt={vid.snippet.title}
/>
<button
className={styles.playBtn}
onClick={onOpenModal}
>
<Image
src="/images/play.svg"
width="60"
height="60"
/>
</button>
<div>
<Modal
open={open}
onClose={onCloseModal}
center
classNames={{
overlay: "customOverlay",
modal: "customModal",
overlayAnimationIn: "customEnterOverlayAnimation",
overlayAnimationOut:
"customLeaveOverlayAnimation",
modalAnimationIn: "customEnterModalAnimation",
modalAnimationOut: "customLeaveModalAnimation",
}}
animationDuration={800}
>
<ReactPlayer
playing={true}
url={
videoURL + `${vid.snippet.resourceId.videoId}`,
}
width="100%"
height="calc(100vh - 100px)"
/>
</Modal>
</div>
</div>
<div className={styles.details_container}>
<h3>{vid.snippet.title}</h3>
</div>
</div>
);
})}
);
}
Update: I tried it by using a second state like this:
const [modalIsOpen, setModalIsOpen] = useState(false);
const [modalData, setModalData] = useState(null);
{sortedVids
.filter((v) => v.snippet.title !== 'Private video')
.map((vid, id) => {
return (
<div className={styles.item_container} key={id}>
<div className={styles.clip_container}>
<Image
className={styles.thumbnails}
src={vid.snippet.thumbnails.medium.url}
layout='fill'
objectFit='cover'
alt={vid.snippet.title}
/>
<button
className={styles.playBtn}
onClick={() => {
setModalData(vid.snippet.resourceId.videoId);
console.log(modalData);
setModalIsOpen(true);
}}
>
<Image src='/images/play.svg' width='60' height='60' />
</button>
<div>
<Modal
key={modalData}
open={modalIsOpen}
onClose={() => setModalIsOpen(false)}
center
classNames={{
overlay: 'customOverlay',
modal: 'customModal',
overlayAnimationIn: 'customEnterOverlayAnimation',
overlayAnimationOut: 'customLeaveOverlayAnimation',
modalAnimationIn: 'customEnterModalAnimation',
modalAnimationOut: 'customLeaveModalAnimation',
}}
animationDuration={800}
>
<ReactPlayer
playing={true}
url={videoURL + `${modalData}`}
width='100%'
height='calc(100vh - 100px)'
/>
</Modal>
</div>
</div>
<div className={styles.details_container}>
<h3>{vid.snippet.title}</h3>
</div>
</div>
);
})}
but the result is the same...