javascript - How to create a popup on click delete button with ReactJS? - Stack Overflow

I want when I click on button delete, it opens a popup of confirmation.I try to use a sweetAlert, but

I want when I click on button delete, it opens a popup of confirmation. I try to use a sweetAlert, but it's not showing any popup.

popupdel method :

 popupdel() {
    swal({
      title: "Are you sure?",
      text: "Your will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonClass: "btn-danger",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    }, function() {
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
  }

delete method :

delete(Code) {

axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {

this.setState({ clients: clients.records });

}.bind(this));

}

<button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>

How can I make the popup showing on click button ?

I want when I click on button delete, it opens a popup of confirmation. I try to use a sweetAlert, but it's not showing any popup.

popupdel method :

 popupdel() {
    swal({
      title: "Are you sure?",
      text: "Your will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonClass: "btn-danger",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    }, function() {
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
  }

delete method :

delete(Code) {

axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {

this.setState({ clients: clients.records });

}.bind(this));

}

<button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>

How can I make the popup showing on click button ?

Share Improve this question edited Jul 20, 2018 at 15:48 Ichrak Mansour asked Jul 19, 2018 at 21:50 Ichrak MansourIchrak Mansour 1,95212 gold badges38 silver badges64 bronze badges 3
  • "I try to use a sweetAlert, but it's not showing any popup." where? – Kevin B Commented Jul 20, 2018 at 15:11
  • @KevinB can you take a look on the post ? I edited it. – Ichrak Mansour Commented Jul 20, 2018 at 15:49
  • 1 i mean.... it still doesn't appear to be part of your delete method. – Kevin B Commented Jul 20, 2018 at 15:50
Add a ment  | 

3 Answers 3

Reset to default 2

Just last week I created my own React wrapper ponent for SweetAlert2. (There were already some wrapper classes out there on NPM, but I didn't like the way they worked, so I made my own.) The following link is a fully-functional example of both A) my wrapper ponent for SweetAlert2, and B) two different ways to launch the alert based on user input.

https://stackblitz./edit/react-sweetalert2-wrapper

The linked example shows how you can launch the alert declaratively (e.g., spell out the JSX code inside the render() function and then toggle a show variable in state), or imperatively (e.g., leave a placeholder for the alert inside the render() function that is dynamically populated with null or the contents of the newly-generated alert).

If I'm understanding what you're trying to achieve:

  1. You could create some state for the popup being shown or not. This would be a boolean.
  2. Set the initial state to false.
  3. Create a method that when you click the button, it toggles the state to true.
  4. When the state is true, display the popup.

Then pass the method down to your button depending on your app structure.

class Example extends React.Component {
  constructor() {
      super();
      this.state = {
          isPopupShown: false
      }

     this.togglePopup = this.togglePopup.bind(this);
  }


  togglePopup() {
    this.setState(prevState => ({
      isPopupShown: !prevState.isPopupShown
    }));
  }

  render() {
    return (
      <div>
        <button onClick={ this.togglePopup }>
          toggle popup
        </button>
        { this.state.isPopupShown === true ? <div>popup shown!</div> : <div>popup hidden!</div> }
      </div>
    )
  }
}

ReactDOM.render(<Example />, document.querySelector("#app"))

Here is a jsfiddle showing how you can toggle something: http://jsfiddle/n5u2wwjg/100862/

Use confirm instead of alert or sweetAlert:

delete(Code) {
    if( confirm('Sure want to delete?')) {
        axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {

    this.setState({ clients: clients.records });
)}

else {
        // Do something..
    } }.bind(this));

    <button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745408224a4626410.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信