Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Sweet Alert popup to button in React component

I found this perfect Sweet Alert module for Bootstrap and React (which I'm using in my Meteor app):

http://djorg83.github.io/react-bootstrap-sweetalert/

But I don't understand how you include this code inside a React component.

When someone clicks the Delete button in my app, I'd like a Sweet Alert prompt to pop up asking for confirmation.

Here is my component for the Delete button:

import React, {Component} from 'react';
import Goals from '/imports/collections/goals/goals.js'
import SweetAlert from 'react-bootstrap-sweetalert';

export default class DeleteGoalButton extends Component {

  deleteThisGoal(){
    console.log('Goal deleted!');
    // Meteor.call('goals.remove', this.props.goalId);
  }

  render(){
    return(
      <div className="inline">
          <a onClick={this.deleteThisGoal()} href={`/students/${this.props.studentId}/}`}
          className='btn btn-danger'><i className="fa fa-trash" aria-hidden="true"></i> Delete Goal</a>
      </div>
    )
  }
}

And here is the code that I copied from the Sweet Alert example:

<SweetAlert
    warning
    showCancel
    confirmBtnText="Yes, delete it!"
    confirmBtnBsStyle="danger"
    cancelBtnBsStyle="default"
    title="Are you sure?"
    onConfirm={this.deleteFile}
    onCancel={this.cancelDelete}
>
    You will not be able to recover this imaginary file!
</SweetAlert>

Anyone know how to do this?

like image 296
Jordan England-Nelson Avatar asked Dec 06 '16 22:12

Jordan England-Nelson


People also ask

How do you add sweet alert on react?

Create a React. Now, open the index. js file and add import Bootstrap. Now install sweetalert2 by using the following command. Now go to src folder and add a new component.

How do you call a sweet alert on button click?

All you have to do is call the swal() function. swal("Here's a message!", " Have a nice day!")

How do I show a popup message in react JS?

Approach: To create our Popup we are going to use the reactjs-popup package because it is powerful, lightweight, and fully customizable. After that, we will add our popup on our homepage with a button to trigger the popup using the installed package. Project Structure: It will look like this.


1 Answers

Working example based on your code http://www.webpackbin.com/VJTK2XgQM

You should use this.setState() and create <SweetAlert ... /> on onClick. You can use fat arrows or .bind() or any other method to be sure that proper context is used.

import React, {Component} from 'react';
import SweetAlert from 'react-bootstrap-sweetalert';

export default class HelloWorld extends Component {

  constructor(props) {
    super(props);

    this.state = {
      alert: null
    };
  } 

  deleteThisGoal() {
    const getAlert = () => (
      <SweetAlert 
        success 
        title="Woot!" 
        onConfirm={() => this.hideAlert()}
      >
        Hello world!
      </SweetAlert>
    );

    this.setState({
      alert: getAlert()
    });
  }

  hideAlert() {
    console.log('Hiding alert...');
    this.setState({
      alert: null
    });
  }

  render() {
    return (
      <div style={{ padding: '20px' }}>
          <a 
            onClick={() => this.deleteThisGoal()}
            className='btn btn-danger'
          >
            <i className="fa fa-trash" aria-hidden="true"></i> Delete Goal
        </a>
        {this.state.alert}
      </div>
    );
  }
}
like image 137
Dawid Karabin Avatar answered Sep 27 '22 17:09

Dawid Karabin