React Antd showing multiple modal on array.map












2














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>
)
}
}









share|improve this question



























    2














    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>
    )
    }
    }









    share|improve this question

























      2












      2








      2







      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>
      )
      }
      }









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 2:09









      Renz

      317




      317
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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 lis and set the item id into state when a particular button is clicked and use that state in the modal.






          share|improve this answer





























            0














            The reason the modal appears many times because




            1. This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that


            2. 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>





            share|improve this answer





















            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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 lis and set the item id into state when a particular button is clicked and use that state in the modal.






            share|improve this answer


























              1














              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 lis and set the item id into state when a particular button is clicked and use that state in the modal.






              share|improve this answer
























                1












                1








                1






                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 lis and set the item id into state when a particular button is clicked and use that state in the modal.






                share|improve this answer












                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 lis and set the item id into state when a particular button is clicked and use that state in the modal.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 2:48









                Ryan C

                923210




                923210

























                    0














                    The reason the modal appears many times because




                    1. This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that


                    2. 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>





                    share|improve this answer





















                    • 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
















                    0














                    The reason the modal appears many times because




                    1. This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that


                    2. 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>





                    share|improve this answer





















                    • 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














                    0












                    0








                    0






                    The reason the modal appears many times because




                    1. This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that


                    2. 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>





                    share|improve this answer












                    The reason the modal appears many times because




                    1. This.showModal of button gets called automatically when Component renders even if you don’t click so you need to restrict that


                    2. 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>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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


















                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'