sweetalert2-react-content
Version:
Official sweetalert2 enhancer adding support for React elements as content.
56 lines (40 loc) • 2.21 kB
Markdown
# sweetalert2-react-content
Official [SweetAlert2](https://sweetalert2.github.io/) enhancer adding support for React elements as content.
[](https://github.com/sweetalert2/sweetalert2-react-content/actions)
[](https://www.npmjs.com/package/sweetalert2-react-content)
[](https://github.com/sweetalert2/sweetalert2-react-content/blob/main/CHANGELOG.md)
[](https://github.com/sweetalert2/sweetalert2-react-content/blob/main/src/sweetalert2-react-content.d.ts)
[](https://opensource.org/licenses/MIT)
The following options can be React elements:
- title
- html
- confirmButtonText
- denyButtonText
- cancelButtonText
- footer
- closeButtonHtml
- iconHtml
- loaderHtml
## Installation
```bash
npm install --save sweetalert2 sweetalert2-react-content
```
## Usage Example
```jsx
import Swal from 'sweetalert2'
import withReactContent from 'sweetalert2-react-content'
const MySwal = withReactContent(Swal)
MySwal.fire({
title: <p>Hello World</p>,
didOpen: () => {
// `MySwal` is a subclass of `Swal` with all the same instance & static methods
MySwal.showLoading()
},
}).then(() => {
return MySwal.fire(<p>Shorthand works too</p>)
})
```
The `dist/sweetalert2-react-content.umd.js` file defines `window.sweetalert2ReactContent` in non-CJS/AMD environments.
## Limitations
SweetAlert2 renders its content outside of the `ReactTree`. In order to render React Router components (such as `Link`) you have to wrap them in the routing context which should be the same with the app.
That can be achived by using the [`HistoryRouter`](https://reactrouter.com/docs/en/v6/routers/history-router) with shared `history`. [Please refer to the official code example ↗️](https://sweetalert2.github.io/recipe-gallery/sweetalert2-react-router.html)