Skip to content
Advertisement

Mapped nested component doesn’t work peopetly (React Js)

I have a list of items mapped to be displayed on my “allItems” page. Now every Item has a button that fires up a modal with the specs of the item, but this modal display the same value for all the item (the last one in the array). I tried to pass the id in toggle func but it doesn’t work.

Anyone knows how can I display the same data in the Card and the Modal?

Here’s my code:

JavaScript

Advertisement

Answer

Issue

You’ve a single boolean modal state that all the modals cue from. When this.state.modal is true then a modal is rendered and opened for each element being mapped.

Solution

Instead of storing a boolean value for whether or not a modal should be open you should store an id when you want a specific modal to open.

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