React : How to display a modal popup only for that specific div
To make it more clear , basically what I'm trying to do is like Amazon
There will be a bunch of products and once you click on the product ,Only that product and its details will be displayed on the Popup Modal.
In my case , I have stored 3 data in an Array, I have mapped it out Which creates 3div and 3Modal Popup with buttons in each.
Once I click on the button of 1st div , I want the modal to be open for that first div only.
But right now when I click on the button all 3 Modal Popup.
I'm new to React , I can do this same thing in JQuery and Javascript, But I'm not able to achieve this in React.
Or is there a better approach to this ? Like rather than looping and creating 3modal popup Can we create just one modal popup, That will display the data of the particular div of which button is being clicked?
My Current Code:
App.js , Where i have created the Array
Product.Js Where its being mapped out into 3div and also has the modal popup
Let me know if you guys need more details
Thank you guys.
3 Div that is being dunamically created with the data from array
But when i Click on any button , popup for all div pops up , Thats the issue
javascript reactjs jsx
|
show 9 more comments
To make it more clear , basically what I'm trying to do is like Amazon
There will be a bunch of products and once you click on the product ,Only that product and its details will be displayed on the Popup Modal.
In my case , I have stored 3 data in an Array, I have mapped it out Which creates 3div and 3Modal Popup with buttons in each.
Once I click on the button of 1st div , I want the modal to be open for that first div only.
But right now when I click on the button all 3 Modal Popup.
I'm new to React , I can do this same thing in JQuery and Javascript, But I'm not able to achieve this in React.
Or is there a better approach to this ? Like rather than looping and creating 3modal popup Can we create just one modal popup, That will display the data of the particular div of which button is being clicked?
My Current Code:
App.js , Where i have created the Array
Product.Js Where its being mapped out into 3div and also has the modal popup
Let me know if you guys need more details
Thank you guys.
3 Div that is being dunamically created with the data from array
But when i Click on any button , popup for all div pops up , Thats the issue
javascript reactjs jsx
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here ifshowModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run ongetModal
traditionally jQuery. so <div> need to have it's id so run it$(id).modal('open');
– Zydnar
Jan 20 at 13:43
|
show 9 more comments
To make it more clear , basically what I'm trying to do is like Amazon
There will be a bunch of products and once you click on the product ,Only that product and its details will be displayed on the Popup Modal.
In my case , I have stored 3 data in an Array, I have mapped it out Which creates 3div and 3Modal Popup with buttons in each.
Once I click on the button of 1st div , I want the modal to be open for that first div only.
But right now when I click on the button all 3 Modal Popup.
I'm new to React , I can do this same thing in JQuery and Javascript, But I'm not able to achieve this in React.
Or is there a better approach to this ? Like rather than looping and creating 3modal popup Can we create just one modal popup, That will display the data of the particular div of which button is being clicked?
My Current Code:
App.js , Where i have created the Array
Product.Js Where its being mapped out into 3div and also has the modal popup
Let me know if you guys need more details
Thank you guys.
3 Div that is being dunamically created with the data from array
But when i Click on any button , popup for all div pops up , Thats the issue
javascript reactjs jsx
To make it more clear , basically what I'm trying to do is like Amazon
There will be a bunch of products and once you click on the product ,Only that product and its details will be displayed on the Popup Modal.
In my case , I have stored 3 data in an Array, I have mapped it out Which creates 3div and 3Modal Popup with buttons in each.
Once I click on the button of 1st div , I want the modal to be open for that first div only.
But right now when I click on the button all 3 Modal Popup.
I'm new to React , I can do this same thing in JQuery and Javascript, But I'm not able to achieve this in React.
Or is there a better approach to this ? Like rather than looping and creating 3modal popup Can we create just one modal popup, That will display the data of the particular div of which button is being clicked?
My Current Code:
App.js , Where i have created the Array
Product.Js Where its being mapped out into 3div and also has the modal popup
Let me know if you guys need more details
Thank you guys.
3 Div that is being dunamically created with the data from array
But when i Click on any button , popup for all div pops up , Thats the issue
javascript reactjs jsx
javascript reactjs jsx
edited Jan 20 at 13:57
Thanveer Shah
asked Jan 20 at 13:17
Thanveer ShahThanveer Shah
415210
415210
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here ifshowModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run ongetModal
traditionally jQuery. so <div> need to have it's id so run it$(id).modal('open');
– Zydnar
Jan 20 at 13:43
|
show 9 more comments
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here ifshowModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run ongetModal
traditionally jQuery. so <div> need to have it's id so run it$(id).modal('open');
– Zydnar
Jan 20 at 13:43
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here if
showModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run on getModal
traditionally jQuery. so <div> need to have it's id so run it $(id).modal('open');
– Zydnar
Jan 20 at 13:43
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here if
showModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run on getModal
traditionally jQuery. so <div> need to have it's id so run it $(id).modal('open');
– Zydnar
Jan 20 at 13:43
|
show 9 more comments
1 Answer
1
active
oldest
votes
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
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single<Modal/>
way.
– Bayu
Jan 20 at 16:46
Just add another demo with Single<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.
– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
No problem. I love coding anyway :). Aboutthis.setState({ showModal: true, dataModal: data });
thedataModal
part is just copydata
record from loop ofthis.props.data.map((data, key)
. SodataModal
will have value something like :{ id: 1, name: "Thanveer" }
, then you pass it asprops
to<Modal/>
component and render it there.
– Bayu
Jan 20 at 22:26
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276832%2freact-how-to-display-a-modal-popup-only-for-that-specific-div%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single<Modal/>
way.
– Bayu
Jan 20 at 16:46
Just add another demo with Single<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.
– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
No problem. I love coding anyway :). Aboutthis.setState({ showModal: true, dataModal: data });
thedataModal
part is just copydata
record from loop ofthis.props.data.map((data, key)
. SodataModal
will have value something like :{ id: 1, name: "Thanveer" }
, then you pass it asprops
to<Modal/>
component and render it there.
– Bayu
Jan 20 at 22:26
|
show 1 more comment
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
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single<Modal/>
way.
– Bayu
Jan 20 at 16:46
Just add another demo with Single<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.
– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
No problem. I love coding anyway :). Aboutthis.setState({ showModal: true, dataModal: data });
thedataModal
part is just copydata
record from loop ofthis.props.data.map((data, key)
. SodataModal
will have value something like :{ id: 1, name: "Thanveer" }
, then you pass it asprops
to<Modal/>
component and render it there.
– Bayu
Jan 20 at 22:26
|
show 1 more comment
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
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
edited Jan 20 at 16:54
answered Jan 20 at 15:38
BayuBayu
538512
538512
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single<Modal/>
way.
– Bayu
Jan 20 at 16:46
Just add another demo with Single<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.
– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
No problem. I love coding anyway :). Aboutthis.setState({ showModal: true, dataModal: data });
thedataModal
part is just copydata
record from loop ofthis.props.data.map((data, key)
. SodataModal
will have value something like :{ id: 1, name: "Thanveer" }
, then you pass it asprops
to<Modal/>
component and render it there.
– Bayu
Jan 20 at 22:26
|
show 1 more comment
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single<Modal/>
way.
– Bayu
Jan 20 at 16:46
Just add another demo with Single<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.
– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
No problem. I love coding anyway :). Aboutthis.setState({ showModal: true, dataModal: data });
thedataModal
part is just copydata
record from loop ofthis.props.data.map((data, key)
. SodataModal
will have value something like :{ id: 1, name: "Thanveer" }
, then you pass it asprops
to<Modal/>
component and render it there.
– Bayu
Jan 20 at 22:26
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
YUS EXACTLY, single modal is exactly what i wanted , But its coming 3 because there is 3 div, So if there is 100 div then there will be 100 Modal, which is not the right way of doing it. So it would be really great if you could show me a working demo of single model for all the div. Thank you so much
– Thanveer Shah
Jan 20 at 16:01
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single
<Modal/>
way.– Bayu
Jan 20 at 16:46
Just make a demo which tried to use your existing code. Please check the update on my answer. Will try to make DEMO for single
<Modal/>
way.– Bayu
Jan 20 at 16:46
Just add another demo with Single
<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.– Bayu
Jan 20 at 16:57
Just add another demo with Single
<Modal/>
element method. Please let me know if you have any question regarding my code. Thanks.– Bayu
Jan 20 at 16:57
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
You are AMAZING, Thank you so much for investing your time on me
– Thanveer Shah
Jan 20 at 17:23
1
1
No problem. I love coding anyway :). About
this.setState({ showModal: true, dataModal: data });
the dataModal
part is just copy data
record from loop of this.props.data.map((data, key)
. So dataModal
will have value something like : { id: 1, name: "Thanveer" }
, then you pass it as props
to <Modal/>
component and render it there.– Bayu
Jan 20 at 22:26
No problem. I love coding anyway :). About
this.setState({ showModal: true, dataModal: data });
the dataModal
part is just copy data
record from loop of this.props.data.map((data, key)
. So dataModal
will have value something like : { id: 1, name: "Thanveer" }
, then you pass it as props
to <Modal/>
component and render it there.– Bayu
Jan 20 at 22:26
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276832%2freact-how-to-display-a-modal-popup-only-for-that-specific-div%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What about global state? Do you use mobX or redux, or new react context API...?
– Zydnar
Jan 20 at 13:27
For me it looks like modal should be separate component and spawning it depends on value showModal. You can laso do it without showModal: boolean - and simply call jQuery modal, but it's messy. Generally jQuery is not really reactive. You can use react-bootstrap package - it don't use jQuery and it's made reactively.
– Zydnar
Jan 20 at 13:29
@Zydnar , I'm new to React so I haven't used redux .etc. Its just react thats all
– Thanveer Shah
Jan 20 at 13:34
Okay , But how will i popup for the particular div? Let me post the picture
– Thanveer Shah
Jan 20 at 13:34
Reactive means variable changes --> all depending views changes / actions are triggered. So you can use method inherited form React.Component - componentReceivedProps - and here if
showModal
would be prop you run your jQuery function. Or simply instead of using this prop from state, let jQuery handle some HTML. So create <div> inside Product component with modal content and run ongetModal
traditionally jQuery. so <div> need to have it's id so run it$(id).modal('open');
– Zydnar
Jan 20 at 13:43