score:0

so what i understand is u rendered each item in the state.elements and each <singleelement/> component has a button and upon clicking on this button u wanna open the modal will display the value of single element

looks like each button is gonna use openmodal method. with this method we have to do 2 things. first change the this.state.modalisopen value and pass the dynamic value on to the modal body. for this, first i ll show u a javascript magic. in javascript

    !!undefined=false
    !!anystring=true  // pretty clear i believe

so first step, this.state.modalisopen:undefined should be set like so. magic will start in third step.

second step you have to pass this value to the singlemodal component. in your parent component u also render a component that includes modal so i will name it **singlemodal**.

        <singlemodal
          modalisopen={this.state.modalisopen} //i just show the related prop here. 
        />

third step in the modal component i believe it is stateless function component. initially in the state modalisopen value was undefined. so with javascript magic !!props.modalisopen value is set to false. so no modal shows up.

fourth step, openmodal method activates the modal.

    openmodal = (beerid) => {
    this.setstate({modalisopen:this.state.elements[beerid] ,    chosenbeer:this.state.beers[beerid]});
      }

here is the thing since i dont know layout about your app, i assumed that beerid is same as the key id value inside each <singleelement key={} />. so i pulled the single element inside the this.state.elements.

here is the second part of the magic. i set the modalisopen to this.state.elements[beerid]. remember that this value is what we set for the isopen prop inside the modal component.

           <modal
            isopen={!!props.modalisopen}
          />

this time props.modalisopen=this.state.elements[beerid] which is a string. so !!props.modalisopen will be true and your modal will open.

last step, now inside the modal

    {props.modalisopen && <p>{props.modalisopen}</p>}

score:1

you can do this two says. when a menu item is clicked created a function that stores the menu item to the state, then the modal can call directly from the state.

what i would do, is have a function attached to the listitem that captures the selected item and sends it through to a custom reusable modal component...

onclick={() => this.handleclick(item)}

handleclick(item) {
 //you would need to create a reusable modal component and import it.
return (
<reusablemodalcomponent item={item}
)

}

then in your reusablemodalcomponent you can access everything from the list via this.props.item.src...etc

update for your example to make your code work as is...

this.state.elements.map((item, index) => {
            return(<singleelement key={index} name={item.name} href={item.href} image={item.image_url} tagline={item.tagline} ondelete={id => this.ondelete(index)} openmodal={elementid => this.openmodal(index)} onclick={() => this.setstate({itemtoshow: item, modalisopen: !this.state.modalisopen}) />)
        })

then, reference this.state.itemtoshow.etc inside your modal


Related Query

More Query from same tag