Installations
npm install react-sweetalert2
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
18.16.1
NPM Version
9.5.1
Statistics
14 Stars
48 Commits
2 Forks
3 Watching
10 Branches
2 Contributors
Updated on 16 Aug 2024
Languages
TypeScript (93.55%)
JavaScript (6.45%)
Total Downloads
Cumulative downloads
Total Downloads
427,903
Last day
-5.1%
1,108
Compared to previous day
Last week
-1.7%
7,189
Compared to previous week
Last month
1.5%
31,039
Compared to previous month
Last year
206.9%
279,730
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
React sweetalert2
Install
$ npm i react-sweetalert2
or
$ yarn add react-sweetalert2
Usage
Functional Component
1 2import React, { useState } from 'react'; 3import SweetAlert2 from 'react-sweetalert2'; 4 5export default function App(){ 6 const [swalProps, setSwalProps] = useState({}); 7 return ( 8 <div> 9 <button onClick={() => { 10 setSwalProps({ 11 show: true, 12 title: 'Basic Usage', 13 text: 'Hello World', 14 }); 15 }}> 16 Open 17 </button> 18 19 <SweetAlert2 {...swalProps} /> 20 </div> 21 ); 22} 23
Class Component
1import React, { Component } from 'react'; 2import SweetAlert2 from 'react-sweetalert2'; 3 4export default class App extends Component{ 5 constructor(){ 6 super(); 7 8 this.state = { 9 swal: {} 10 } 11 } 12 13 render() { 14 return ( 15 <div> 16 <button onClick={() => { 17 this.setState({ 18 swal: { 19 show: true, 20 title: 'Basic Usage', 21 text: 'Hello World' 22 } 23 }); 24 }}>Alert</button> 25 <SweetAlert2 {...this.state.swal} /> 26 </div> 27 ); 28 } 29} 30 31 32
Using withSwal
function
Inject swal
props into Functional Component
1import React from 'react'; 2import { withSwal } from 'react-sweetalert2'; 3 4export default withSwal((props, ref) => { 5 const { swal, ...rest } = props; 6 7 function handleClick(){ 8 swal.fire({ 9 title: 'Example', 10 text: 'Swal injected', 11 icon: 'success', 12 }); 13 } 14 15 return ( 16 <button onClick={handleClick}> 17 Open 18 </button> 19 ); 20}); 21
Inject swal
props into Class Component
1import React from 'react'; 2import { withSwal } from 'react-sweetalert2'; 3 4class ExampleComponent extends Component { 5 6 function handleClick(){ 7 this.swal.fire({ 8 title: 'Example', 9 text: 'Swal injected', 10 icon: 'success', 11 }); 12 } 13 14 render(){ 15 return ( 16 <button onClick={this.handleClick.bind(this)}> 17 Open 18 </button> 19 ); 20 } 21} 22 23export default withSwal(ExampleComponent); 24
Events
Using SweetAlert2
component
1import React, { useState } from 'react'; 2import SweetAlert2 from 'react-sweetalert2'; 3 4export default function App(){ 5 const [swalProps, setSwalProps] = useState({}); 6 7 function handleClick(){ 8 setSwalProps({ 9 show: true, 10 title: 'Example', 11 text: 'Hello World', 12 }); 13 } 14 15 return ( 16 <div> 17 <button onClick={handleClick}> 18 Alert 19 </button> 20 <SweetAlert2 {...swalProps} 21 didOpen={() => { 22 // run when swal is opened... 23 }} 24 didClose={() => { 25 // run when swal is closed... 26 }} 27 onConfirm={result => { 28 // run when clieked in confirm and promise is resolved... 29 }} 30 onError={error => { 31 // run when promise rejected... 32 }} 33 onResolve={result => { 34 // run when promise is resolved... 35 }} 36 /> 37 </div> 38 ); 39}
Using swal
prop injected
1import React from 'react'; 2import { withSwal } from 'react-sweetalert2'; 3 4export default withSwal(({ swal }, ref) => ( 5 <button onClick={e => { 6 swal.fire({ 7 title: 'Example', 8 text: 'Hello World', 9 didOpen: () => { 10 // run when swal is opened... 11 }, 12 didClose: () => { 13 // run when swal is closed... 14 } 15 }).then(result => { 16 // when confirmed and promise resolved... 17 }).catch(error => { 18 // when promise rejected... 19 }); 20 }}> 21 Show Alert 22 </button> 23)); 24
Using content from HTML and React elements
1import React, { useState } from 'react'; 2import SweetAlert2 from 'react-sweetalert2'; 3 4export default function App(){ 5 const [swalProps, setSwalProps] = useState({}); 6 7 function handleClick(){ 8 setSwalProps({ 9 show: true, 10 title: 'Example' 11 }); 12 } 13 14 return ( 15 <div> 16 <button onClick={handleClick}> 17 Alert 18 </button> 19 <SweetAlert2 {...swalProps}> 20 <h1> 21 Hello World! 22 </h1> 23 </SweetAlert2> 24 </div> 25 ); 26} 27
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/kessejones/react-sweetalert2/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/kessejones/react-sweetalert2/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/kessejones/react-sweetalert2/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/kessejones/react-sweetalert2/test.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:32
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
- Warn: branch protection not enabled for branch 'v0.1.3'
- Warn: branch protection not enabled for branch 'v0.1.2'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-457r-cqc8-9vj9
- Warn: Project is vulnerable to: GHSA-8jh9-wqpf-q52c
- Warn: Project is vulnerable to: GHSA-mrr8-v49w-3333
- Warn: Project is vulnerable to: GHSA-pg98-6v7f-2xfv
- Warn: Project is vulnerable to: GHSA-qq6h-5g6j-q3cm
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.5
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-sweetalert2
sweetalert2
A beautiful, responsive, customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes, supported fork of sweetalert
sweetalert2-react-content
Official sweetalert2 enhancer adding support for React elements as content.
vue-sweetalert2
Simple Vue sweetalert2 package
@sweetalert2/theme-bootstrap-4
Bootstrap 4 theme for SweetAlert2