Sweet Alert is used to make an alert box more attractive and easier to design. The sweet JS Provides easy methods to design and add a lot of functionality to the alert box of the website by just calling the function of sweet alert (in short SWAL()).
It is a beautiful replacement for JavaScript confirming the message! It will replace the ugly confirmation message with a beautiful customizable and fully functional modal.
SweetAlert 2 is advance of sweetAlert. SweetAlert2 supports HTML content as well while the former does not.
There is an independent dependency that needs to be installed to work with SweetAlert2.
Syntax:
function SweetAlert2() { const fireAlert = () => { Swal.fire({ ... } ).then((result) => { ... }) } }
Let’s build a react project and show the working of sweetAlert2
To build a react application follow the below steps:
Step 1: Create a react application using the following command Â
npx create-react-app foldername
Step 2: Once it is done change your directory to the newly created application using the following command Â
cd foldername
Step 3: Install Required Dependency
npm install sweetalert2
Step to run the application: Enter the following command to run the application.
npm start
Project Structure: The project should look like this:
Example 1: In this example, we will simply display sweetAlert2. On success, it will display a message, ‘Nice to meet you’ and on cancel it will display, ‘Cancelled’. Write the below code in App.js
Javascript
import React from 'react' import Swal from 'sweetalert2' import { useState } from 'react' function SweetAlert2() { Â Â Â Â const fireAlert = () => { Â Â Â Â Â Â Â Â Swal.fire({ Â Â Â Â Â Â Â Â Â Â Â Â title: 'I am Sweet Alert 2.' , Â Â Â Â Â Â Â Â Â Â Â Â showConfirmButton: true , Â Â Â Â Â Â Â Â Â Â Â Â showCancelButton: true , Â Â Â Â Â Â Â Â Â Â Â Â confirmButtonText: "OK" , Â Â Â Â Â Â Â Â Â Â Â Â cancelButtonText: "Cancel" , Â Â Â Â Â Â Â Â Â Â Â Â icon: 'warning' Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â ).then((result) => { Â Â Â Â Â Â Â Â Â Â Â Â /* Read more about isConfirmed, isDenied below */ Â Â Â Â Â Â Â Â Â Â Â Â if (result.isConfirmed) { Â
                Swal.fire( 'Nice to meet you' , '' , 'success' ); Â
            } else                 Swal.fire( ' Cancelled' , '' , 'error' ) Â
        })     }     return (         <div >             <center>                 <button className= "btn btn-primary"                     onClick={fireAlert}>                     Click me to see Sweet Alert 2                 </button>             </center>         </div>     ) } Â
export default function App() {     return (         <div className= "App" >             <h1 style={{ color: 'green' }}>                 neveropen             </h1>             <h3>SweetAlert2 in React</h3>             <SweetAlert2 />         </div>     ); } |
Output:
Â
Example 2: In this example, we will display a counter. An Alert will be popUp which will ask to increment the value of the counter.
Javascript
import React from 'react' import Swal from 'sweetalert2' import { useState } from 'react' function SweetAlert2() { Â Â Â Â const [counter, setCounter] = useState(0); Â Â Â Â const fireAlert = () => { Â Â Â Â Â Â Â Â Swal.fire({ Â Â Â Â Â Â Â Â Â Â Â Â title: 'I am Sweet Alert 2.' , Â Â Â Â Â Â Â Â Â Â Â Â html: ' Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â < p > Can I increase counter?</p> Â Â Â Â Â Â Â Â Â Â Â Â ' , Â Â Â Â Â Â Â Â Â Â Â Â showConfirmButton: true , Â Â Â Â Â Â Â Â Â Â Â Â showCancelButton: true , Â Â Â Â Â Â Â Â Â Â Â Â confirmButtonText: "Yes Increase" , Â Â Â Â Â Â Â Â Â Â Â Â cancelButtonText: "Cancel" , Â Â Â Â Â Â Â Â Â Â Â Â icon: 'warning' Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â ).then((result) => { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* Read more about isConfirmed, isDenied below */ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (result.isConfirmed) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â setCounter(counter + 1) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Swal.fire( 'Counter Value Increased' , '' , 'success' ); Â
                } else                     Swal.fire( ' Cancelled' , '' , 'error' )             }) } return (     <div >         <center>             <br></br>             <strong> Counter Value:   </strong>             <div style={{                 padding: '2%' ,                 background: '#308D46' ,                 color: 'white' ,                 fontWeight: 'bold' ,                 borderRadius: '4%' ,                 display: 'inline-block'             }}>                 {counter}             </div>             <br></br>             <br></br>             <button className= "btn btn-primary"                 onClick={fireAlert}>                 Click me to see Sweet Alert 2             </button>         </center>     </div> ) } Â
export default function App() {     return (         <div className= "App" >             <h1 style={{ color: 'green' }}>                 neveropen             </h1>             <h3>SweetAlert2 in React</h3>             <SweetAlert2 />         </div>     ); } |
Output:
Â