Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kessejones/react-sweetalert2

SweetAlert2 implementation for ReactJS
https://github.com/kessejones/react-sweetalert2

front-end javascript react reactjs sweetalert2 web

Last synced: 3 months ago
JSON representation

SweetAlert2 implementation for ReactJS

Awesome Lists containing this project

README

        

[![Test](https://github.com/kessejones/react-sweetalert2/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/kessejones/react-sweetalert2/actions/workflows/test.yml)
[![Coverage Status](https://img.shields.io/coveralls/github/kessejones/react-sweetalert2)](https://coveralls.io/github/kessejones/react-sweetalert2?branch=master)

# React sweetalert2

## Install

```
$ npm i react-sweetalert2
```
or
```
$ yarn add react-sweetalert2
```

## Usage

#### Functional Component

```javascript

import React, { useState } from 'react';
import SweetAlert2 from 'react-sweetalert2';

export default function App(){
const [swalProps, setSwalProps] = useState({});
return (


{
setSwalProps({
show: true,
title: 'Basic Usage',
text: 'Hello World',
});
}}>
Open



);
}

```

#### Class Component
```javascript
import React, { Component } from 'react';
import SweetAlert2 from 'react-sweetalert2';

export default class App extends Component{
constructor(){
super();

this.state = {
swal: {}
}
}

render() {
return (


{
this.setState({
swal: {
show: true,
title: 'Basic Usage',
text: 'Hello World'
}
});
}}>Alert


);
}
}

```
#### Using `withSwal` function

##### Inject `swal` props into Functional Component

```javascript
import React from 'react';
import { withSwal } from 'react-sweetalert2';

export default withSwal((props, ref) => {
const { swal, ...rest } = props;

function handleClick(){
swal.fire({
title: 'Example',
text: 'Swal injected',
icon: 'success',
});
}

return (

Open

);
});

```

##### Inject `swal` props into Class Component

```javascript
import React from 'react';
import { withSwal } from 'react-sweetalert2';

class ExampleComponent extends Component {

function handleClick(){
this.swal.fire({
title: 'Example',
text: 'Swal injected',
icon: 'success',
});
}

render(){
return (

Open

);
}
}

export default withSwal(ExampleComponent);

```

#### Events

##### Using `SweetAlert2` component

```javascript
import React, { useState } from 'react';
import SweetAlert2 from 'react-sweetalert2';

export default function App(){
const [swalProps, setSwalProps] = useState({});

function handleClick(){
setSwalProps({
show: true,
title: 'Example',
text: 'Hello World',
});
}

return (



Alert

{
// run when swal is opened...
}}
didClose={() => {
// run when swal is closed...
}}
onConfirm={result => {
// run when clieked in confirm and promise is resolved...
}}
onError={error => {
// run when promise rejected...
}}
onResolve={result => {
// run when promise is resolved...
}}
/>

);
}
```

##### Using `swal` prop injected

```javascript
import React from 'react';
import { withSwal } from 'react-sweetalert2';

export default withSwal(({ swal }, ref) => (
{
swal.fire({
title: 'Example',
text: 'Hello World',
didOpen: () => {
// run when swal is opened...
},
didClose: () => {
// run when swal is closed...
}
}).then(result => {
// when confirmed and promise resolved...
}).catch(error => {
// when promise rejected...
});
}}>
Show Alert

));

```

##### Using content from HTML and React elements

```javascript
import React, { useState } from 'react';
import SweetAlert2 from 'react-sweetalert2';

export default function App(){
const [swalProps, setSwalProps] = useState({});

function handleClick(){
setSwalProps({
show: true,
title: 'Example'
});
}

return (



Alert



Hello World!




);
}

```