Dynamically Changing React-modal Content
Solution 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 (
<ReusableModalComponentitem={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
Solution 2:
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.
<ModalisOpen={!!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>}
Post a Comment for "Dynamically Changing React-modal Content"