React Antd showing multiple modal on array.map
When clicking the update button the modal pops twice, the first one shows the correct item.id but the second one is the last value on the list. any help is appreciated. tried adding {this.props.child} inside the modal tag but it doesn't work.
any help would be appreciated.
this.state = {
ModalText: 'Content of the modal',
visible: false,
currentId : 0,
confirmLoading: false,
}
}
showModal = () => {
this.setState({
visible: true,
});
}
handleOk = () => {
this.setState({
ModalText: 'The modal will be closed after two seconds',
confirmLoading: true,
});
setTimeout(() => {
this.setState({
visible: false,
confirmLoading: false,
});
}, 2000);
}
handleCancel = () => {
console.log('Clicked cancel button');
this.setState({
visible: false,
});
}
render(){
const { visible, confirmLoading, ModalText } = this.state;
return(
<div>
<ul>
{this.props.user.map(item => (
<li key={item.id}>
The person is {item.name} and the his/her email is
{item.email}.
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
</li>
))}
</ul>
</div>
)
}
}
reactjs antd
add a comment |
When clicking the update button the modal pops twice, the first one shows the correct item.id but the second one is the last value on the list. any help is appreciated. tried adding {this.props.child} inside the modal tag but it doesn't work.
any help would be appreciated.
this.state = {
ModalText: 'Content of the modal',
visible: false,
currentId : 0,
confirmLoading: false,
}
}
showModal = () => {
this.setState({
visible: true,
});
}
handleOk = () => {
this.setState({
ModalText: 'The modal will be closed after two seconds',
confirmLoading: true,
});
setTimeout(() => {
this.setState({
visible: false,
confirmLoading: false,
});
}, 2000);
}
handleCancel = () => {
console.log('Clicked cancel button');
this.setState({
visible: false,
});
}
render(){
const { visible, confirmLoading, ModalText } = this.state;
return(
<div>
<ul>
{this.props.user.map(item => (
<li key={item.id}>
The person is {item.name} and the his/her email is
{item.email}.
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
</li>
))}
</ul>
</div>
)
}
}
reactjs antd
add a comment |
When clicking the update button the modal pops twice, the first one shows the correct item.id but the second one is the last value on the list. any help is appreciated. tried adding {this.props.child} inside the modal tag but it doesn't work.
any help would be appreciated.
this.state = {
ModalText: 'Content of the modal',
visible: false,
currentId : 0,
confirmLoading: false,
}
}
showModal = () => {
this.setState({
visible: true,
});
}
handleOk = () => {
this.setState({
ModalText: 'The modal will be closed after two seconds',
confirmLoading: true,
});
setTimeout(() => {
this.setState({
visible: false,
confirmLoading: false,
});
}, 2000);
}
handleCancel = () => {
console.log('Clicked cancel button');
this.setState({
visible: false,
});
}
render(){
const { visible, confirmLoading, ModalText } = this.state;
return(
<div>
<ul>
{this.props.user.map(item => (
<li key={item.id}>
The person is {item.name} and the his/her email is
{item.email}.
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
</li>
))}
</ul>
</div>
)
}
}
reactjs antd
When clicking the update button the modal pops twice, the first one shows the correct item.id but the second one is the last value on the list. any help is appreciated. tried adding {this.props.child} inside the modal tag but it doesn't work.
any help would be appreciated.
this.state = {
ModalText: 'Content of the modal',
visible: false,
currentId : 0,
confirmLoading: false,
}
}
showModal = () => {
this.setState({
visible: true,
});
}
handleOk = () => {
this.setState({
ModalText: 'The modal will be closed after two seconds',
confirmLoading: true,
});
setTimeout(() => {
this.setState({
visible: false,
confirmLoading: false,
});
}, 2000);
}
handleCancel = () => {
console.log('Clicked cancel button');
this.setState({
visible: false,
});
}
render(){
const { visible, confirmLoading, ModalText } = this.state;
return(
<div>
<ul>
{this.props.user.map(item => (
<li key={item.id}>
The person is {item.name} and the his/her email is
{item.email}.
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
</li>
))}
</ul>
</div>
)
}
}
reactjs antd
reactjs antd
asked Nov 21 at 2:09
Renz
317
317
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think if you were to look at the DOM via developer tools, you would find that the modal didn't pop up twice, but rather once per item and that the one showing the last item is just the one on top. All of your modals (you are rendering one per item) are being controlled by the same visible
boolean in your state, so setting that to true
by clicking any of the buttons in the list will show all of the modals.
There are a few different ways you could fix this. One option is to have a single modal outside of the li
s and set the item id into state when a particular button is clicked and use that state in the modal.
add a comment |
The reason the modal appears many times because
This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that
You are always showing modal without any conditional check in loop so you need conditional check for Modal
So Change
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
To
<Button type="primary" onClick={() => this.showModal()}> //made changes here
Update
</Button>
//added change here —> {visible && <Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>}
</Modal>
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
add a 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%2f53404378%2freact-antd-showing-multiple-modal-on-array-map%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think if you were to look at the DOM via developer tools, you would find that the modal didn't pop up twice, but rather once per item and that the one showing the last item is just the one on top. All of your modals (you are rendering one per item) are being controlled by the same visible
boolean in your state, so setting that to true
by clicking any of the buttons in the list will show all of the modals.
There are a few different ways you could fix this. One option is to have a single modal outside of the li
s and set the item id into state when a particular button is clicked and use that state in the modal.
add a comment |
I think if you were to look at the DOM via developer tools, you would find that the modal didn't pop up twice, but rather once per item and that the one showing the last item is just the one on top. All of your modals (you are rendering one per item) are being controlled by the same visible
boolean in your state, so setting that to true
by clicking any of the buttons in the list will show all of the modals.
There are a few different ways you could fix this. One option is to have a single modal outside of the li
s and set the item id into state when a particular button is clicked and use that state in the modal.
add a comment |
I think if you were to look at the DOM via developer tools, you would find that the modal didn't pop up twice, but rather once per item and that the one showing the last item is just the one on top. All of your modals (you are rendering one per item) are being controlled by the same visible
boolean in your state, so setting that to true
by clicking any of the buttons in the list will show all of the modals.
There are a few different ways you could fix this. One option is to have a single modal outside of the li
s and set the item id into state when a particular button is clicked and use that state in the modal.
I think if you were to look at the DOM via developer tools, you would find that the modal didn't pop up twice, but rather once per item and that the one showing the last item is just the one on top. All of your modals (you are rendering one per item) are being controlled by the same visible
boolean in your state, so setting that to true
by clicking any of the buttons in the list will show all of the modals.
There are a few different ways you could fix this. One option is to have a single modal outside of the li
s and set the item id into state when a particular button is clicked and use that state in the modal.
answered Nov 21 at 2:48
Ryan C
923210
923210
add a comment |
add a comment |
The reason the modal appears many times because
This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that
You are always showing modal without any conditional check in loop so you need conditional check for Modal
So Change
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
To
<Button type="primary" onClick={() => this.showModal()}> //made changes here
Update
</Button>
//added change here —> {visible && <Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>}
</Modal>
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
add a comment |
The reason the modal appears many times because
This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that
You are always showing modal without any conditional check in loop so you need conditional check for Modal
So Change
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
To
<Button type="primary" onClick={() => this.showModal()}> //made changes here
Update
</Button>
//added change here —> {visible && <Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>}
</Modal>
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
add a comment |
The reason the modal appears many times because
This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that
You are always showing modal without any conditional check in loop so you need conditional check for Modal
So Change
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
To
<Button type="primary" onClick={() => this.showModal()}> //made changes here
Update
</Button>
//added change here —> {visible && <Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>}
</Modal>
The reason the modal appears many times because
This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that
You are always showing modal without any conditional check in loop so you need conditional check for Modal
So Change
<Button type="primary" onClick={this.showModal}>
Update
</Button>
<Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>
</Modal>
To
<Button type="primary" onClick={() => this.showModal()}> //made changes here
Update
</Button>
//added change here —> {visible && <Modal title="Title"
visible={visible}
onOk={this.handleOk}
confirmLoading={confirmLoading}
onCancel={this.handleCancel}
>
{item.id}
<UpdateModal/>}
</Modal>
answered Nov 21 at 2:57
Hemadri Dasari
7,23111139
7,23111139
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
add a comment |
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
showModal is not being executed during render. The function reference is being passed as the click handler, it would need parentheses in order to cause it to be called during render.
– Ryan C
Nov 21 at 3:14
add a 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53404378%2freact-antd-showing-multiple-modal-on-array-map%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