Skip to content
Advertisement

Multiple buttons triggering the same modal component

I have an videos array, which in turn has objects of type Video (typing below).

I need that when clicking on the button corresponding to a specific video, I can open only one modal with the information of the clicked video.

JavaScript

Below is the component that renders the array of videos through a map, notice that inside the map, I have an onClick function that calls the modal.

JavaScript

And below the VideoModal component:

JavaScript

I understand that the modal uses the “open” property to define whether it is open or not, but when I click the button and perform the setModalOpen, it renders a modal for each object in the array. I don’t understand how I could assemble this correctly.

Advertisement

Answer

I solved it as follows, created a state called videoToModal of type VideosInfo and a function called handleModalOpen, passed the video parameter to the function, and in the function stored this video in the videoToModal state.

I instantiated the VideoModal component outside the map (obviously should have done this before) and passed the state to the VideoModal component’s video parameter.

Below is the complete code for the component.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement