score:8
Of course, all modal will pop up at the same time. All modal using exactly same state which is this.state.showModal
. Once it gets true
then all will just pop up. If you still like to have 3 modals like that. I suggest you to make the value of showModal
state with JSON value. Maybe something like this:
state = {
showModal: {}
}
then for getModal()
function:
getModal = value => {//still using variable from `data.id`
let key_to_update = {};
key_to_update[value] = true;
this.setState( {
showModal: Object.assign( {}, this.state.showModal, key_to_update )
} );
}
Then for the <Modal/>
should looks like this:
<Modal show={this.state.showModal[data.id]} onClick={()=> this.hideModal(data.id)}/>
To hide the modal you can do opposite of getModal()
as follow:
hideModal = value => {//still using variable from `data.id`
let key_to_update = {};
key_to_update[value] = false;//Just different on this
this.setState( {
showModal: Object.assign( {}, this.state.showModal, key_to_update )
} );
}
If you still interested and have a problem to implement it, I can help you create a working demo. Because I am not really test the code, just make it based on my experience and quick analysis. However, personally, I like to have a single Modal for this kind of case. Just set a single "state" of "Product detail" then read that "state" from single Modal then show it at the same time.
==== DEMO: MULTIPLE MODAL ELEMENT TECHNIQUE =====
Just like your comment, because you only need to show single modal at a time, then it will be much easier. We don't need to have multiple true/false condition like above. We can just use data.id
as the true/false check to the showModal
state like follow:
class Product extends Component {
state = {
showModal: 0
};
getModal = value => {
this.setState({ showModal: value });
};
hideModal = value => {
this.setState({ showModal: 0 });
};
render() {
return (
<div className="container">
{this.props.data.map((data, key) => (
<div key={key} className="small">
<p>Namsse: {data.name}</p>
<button onClick={() => this.getModal(data.id)}>Popup</button>
<Modal
show={this.state.showModal === data.id}
onHide={() => this.hideModal(data.id)}
name={data.name}
/>
</div>
))}
</div>
);
}
}
Working Demo: https://codesandbox.io/s/pkjvy72mw0
===DEMO: SINGLE MODAL ELEMENT TECHNIQUE===
You can also have only single <Modal/>
element just like below:
class Product extends Component {
state = {
showModal: false,
dataModal: {
name: ""
}
};
getModal = data => {
this.setState({ showModal: true, dataModal: data });
};
hideModal = () => {
this.setState({ showModal: false });
};
render() {
return (
<div className="container">
{this.props.data.map((data, key) => (
<div key={key} className="small">
<p>Namsse: {data.name}</p>
<button onClick={() => this.getModal(data)}>Popup</button>
</div>
))}
<Modal
show={this.state.showModal}
onHide={this.hideModal}
name={this.state.dataModal.name}
/>
</div>
);
}
}
Working demo: https://codesandbox.io/s/53x7m726xk
Source: stackoverflow.com
Related Query
- React : How to display a modal popup only for that specific div
- How to display a div only for certain urls in react js
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- How to reuse a function for multiple select options that only use a single event handler in React
- How to display a div element on a specific key in a map in react
- How to create a div with arrow and divider for content such that they are aligned using react and css?
- how to render useState values for only specific div (component)?
- How to parse the URL & check if that has https & display only the hostname in react
- How to wait for AJAX response and only after that render the component?
- How to simulate mouse over event on a div using enzyme for testing a react application?
- React show Material-UI Tooltip only for text that has ellipsis
- React CSS - how to apply CSS to specific pages only
- How can I write a unit test for a react component that calls reduxjs's mapStateToProps?
- How to make React input onChange set state only after onChange stops firing for set time?
- How can I search for a component with specific key in React Developer Tools?
- How to toggle class for the only one element on click in react
- How to set display name for a React class (ES6)?
- Only Display Component for Some Routes - React
- How to conditionally render a DIV and REACT component for OPENING and CLOSING tags?
- Prevent browser back button for a specific page only in react
- calendar Ant Design: how to show event only in specific day in month using React JS
- React: [useRef, scrollIntoView,] How to only autoscroll a specific div inside of an overflowing page?
- How to display an image that required bearer token to access it in react
- How do I return a json file from s3 to a specific url, but only that url
- How to set a Modal popup with an image in React
- How to dynamically create button for calling specific action using map array in react
- How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component?
- How to add "plus-button" on top right in material-table for react and call my specific function in react
- React How to return div and variable value for another component
- How to display and handle dynamic checkoxes that are dependent on Task array value in the backend (Mongodb) in react js?
More Query from same tag
- Why is an If statement considered a function in this loop?
- Getting data from Firebase not displaying with React JS?
- ReactJS : infinite loop rendering component
- React.js, this.props.changeTitle is not a function
- react route component is not rendering anything
- How to get country code and Country name using IP in react js
- Getting User Input in React Js in Fetch statement
- Edit custom column component while adding new row of Material Table
- Using a custom hook I am unable to refresh/re-render the component
- Is bind(this) necessary in Thinking in React tutorial example?
- nested object missing after reducer state change
- How can I add a class to a button when a Formik Form isSubmitting?
- Why should one use React with Meteor?
- Generics that extend a static type aren't intellisensing
- React Bootstrap change link of Navbar to active
- Organize Redux action type and state
- Render a new Input field on Validating User Input
- how to update formik input value using state
- How to api call in onChange function on CheckBox field React js
- React page not updating on socket message push despite the data being sent
- GitHub gh-pages to work with the app domain name?
- Linting error on creating material icon clickable
- Sort array based on string transformed into date
- How to avoid TypeError: Cannot read property 'charAt' of undefined when the json input is not available?
- ReactJS console.log disappearing after some second
- React Redux Reducers and Immutable Updates
- React app with wordrpess on nginx won't work on HTTPS
- checked box need to be click 2 time to make it check i have tried with onclick as well it is giving same behaviour i need to disable button oncheck
- React props using Meteor Apollo
- Enable button based on user ID