sweetalert2 / sweetalert2-react-content

Official SweetAlert2 enhancer adding support for React elements as content
MIT License
701 stars 48 forks source link

can you pass react components in html prop? #194

Closed DanielVain closed 2 years ago

DanielVain commented 2 years ago
Screen Shot 2022-05-09 at 20 53 12

using Link component here breaks my alert and it shows nothing except the title.

does Sweetalert2-react-content support react components as html content?

limonte commented 2 years ago

Here's the official example of how to use SweetAlert2 + React Router: https://sweetalert2.github.io/recipe-gallery/sweetalert2-react-router.html


⚠️ ⚠️ ⚠️ THE EXAMPLE BELOW IS INCORRECT ⚠️ ⚠️ ⚠️

My guess is that you're using React Router. If so you need to wrap what you're passing to html param into BrowserRouter:

MySwal.fire({
  html: (
    <BrowserRouter>
      ...
    </BrowserRouter>
  ),
})
GreenAsJade commented 2 years ago

^^ This advice is "wrong", in the sense that if you do this you are creating a second routing context, which won't work the way you want.

It will render the <Link>, but the rendered link won't do anything when you click on it.

https://stackoverflow.com/a/72690830/554807

limonte commented 2 years ago

Here's the official example of how to use SweetAlert2 + React Router: https://sweetalert2.github.io/recipe-gallery/sweetalert2-react-router.html