Introduction

In this article, we will learn how to use sweetalert2 in ReactJS Application.sweetalert2 to display an alert message.
Prerequisites
  • Basic Knowledge of ReactJS
  • Visual Studio Code
  • Node and NPM installed
  • Bootstrap

Create a React.js Project

To create a new ReactJS project, open the command prompt and enter the following command.
  1. npx create-reactp-reduxapp
Open this project in Visual Studio Code and install Bootstrap by using the following command.
  1. npm install bootstrap --save
Now, open the index.js file and add import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css';
Install sweetalert2
Now install sweetalert2 by using the following command.
  1. npm install --save sweetalert2 sweetalert2-react-content
Now go to src folder and add a new component.
  • Sweetalertdemo.js
Now, open the Sweetalertdemo.js file and add the following code.
  1. import React, { Component } from "react";
  2. import Swal from "sweetalert2";
  3. export default class Sweetalertdemo extends Component {
  4. constructor() {
  5. super();
  6. this.HandleClick = this.HandleClick.bind(this);
  7. }
  8. HandleClick() {
  9. Swal.fire({
  10. title: 'Success',
  11. type: 'success',
  12. text: 'Your work has been saved.',
  13. });
  14. }
  15. HandleClick1() {
  16. Swal.fire({
  17. title: 'Are you sure?',
  18. text: 'User will have Admin Privileges',
  19. icon: 'warning',
  20. showCancelButton: true,
  21. confirmButtonColor: '#3085d6',
  22. cancelButtonColor: '#d33',
  23. confirmButtonText: 'Yes!'
  24. });
  25. }
  26. HandleClick12() {
  27. Swal.fire({
  28. icon: 'error',
  29. title: 'Oops...',
  30. text: 'Something went wrong!',
  31. footer: '<a href>Why do I have this issue?</a>'
  32. });
  33. }
  34. HandleClicktop() {
  35. Swal.fire({
  36. position: 'top-end',
  37. icon: 'success',
  38. title: 'Your work has been saved',
  39. showConfirmButton: false,
  40. timer: 1500
  41. });
  42. }
  43. HandleClickAutoclose() {
  44. let timerInterval
  45. Swal.fire({
  46. title: 'Auto close alert!',
  47. html: 'I will close in <b></b> milliseconds.',
  48. timer: 1000,
  49. timerProgressBar: true,
  50. onBeforeOpen: () => {
  51. Swal.showLoading()
  52. timerInterval = setInterval(() => {
  53. const content = Swal.getContent()
  54. if (content) {
  55. const b = content.querySelector('b')
  56. if (b) {
  57. b.textContent = Swal.getTimerLeft()
  58. }
  59. }
  60. }, 100)
  61. },
  62. onClose: () => {
  63. clearInterval(timerInterval)
  64. }
  65. }).then((result) => {
  66. if (result.dismiss === Swal.DismissReason.timer) {
  67. console.log('I was closed by the timer')
  68. }
  69. })
  70. }
  71. render() {
  72. return (
  73. <div>
  74. <div class="row" className="hdr">
  75. <div class="col-sm-12 btn btn-info">
  76. SweetAlert2 In React
  77. </div>
  78. </div>
  79. <div style={{ "paddingTop": "10px" }}>
  80. <button class="btn btn-info btn" onClick={this.HandleClick}>Success</button>
  81. <button class="btn btn-success btn" onClick={this.HandleClick1}>Confirm</button>
  82. <button class="btn btn-primary btn" onClick={this.HandleClick12}>Confirm Box</button>
  83. <button class="btn btn-primary btn" onClick={this.HandleClicktop}>Top Side</button>
  84. <button class="btn btn-primary btn" onClick={this.HandleClickAutoclose}>Auto Close</button>
  85. </div>
  86. </div>
  87. );
  88. }
  89. }
Open app.css file, add the following css.
  1. .btn
  2. {margin-right: 10px;margin-left: 10px}
Open the App.js file and add the following code.
  1. import React from 'react';
  2. import './App.css';
  3. import Sweetalertdemo from './Sweetalertdemo'
  4. function App() {
  5. return (
  6. <div className="App">
  7. <Sweetalertdemo/>
  8. </div>
  9. );
  10. }
  11. export default App;
Now, run the project by using 'npm start' command and check the result.
How To Use Sweetalert2 in React Application
How To Use Sweetalert2 in React Application
How To Use Sweetalert2 in React Application

Summary


In this article, we learned how to use sweetalert2 in ReactJS applications.sweetalert2 to display an alert message.