kessejones / react-sweetalert2

SweetAlert2 implementation for ReactJS
https://kessejones.github.io/react-sweetalert2/
Apache License 2.0
14 stars 2 forks source link

Possible to pass component to sweet alert? #4

Closed RayHughes closed 5 years ago

RayHughes commented 5 years ago

Is it possible to pass a component to sweet alert. I have tried to no avail.

kessejones commented 5 years ago

This is not yet supported. But I plan to release a new version soon, and I can do this functionality.

RayHughes commented 5 years ago

Great thanks!

RayHughes commented 5 years ago

I saw you closed this. Was this added?

kessejones commented 5 years ago

Wouldn't that be what you need?

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',
            type: 'success',
        });
    }

    return (
        <button onClick={handleClick}>
            Open
        </button>
    );
});
RayHughes commented 5 years ago

So I was trying to get SWAL to render a component in the html area. The reason for this is because I need to be able to copy a value in the alert to a users clipboard.

<SweetAlert2 Component={component}/>
kessejones commented 5 years ago

Hmm, I understand what you do but I don't think it's possible. The SWAL html area expects to receive a string, I believe it is not possible to render a component inside it.

pbldmngz commented 3 years ago

Since 2 years have passed, did this feature get included?

rohit-9823 commented 2 years ago

its 2022 is this feature included yet??

kessejones commented 2 years ago

Yes, it is possible to pass a component to swal, but it must not share the state component with the parent component. It can be used like this:

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 (
        <div>   
            <button onClick={handleClick}>
                Alert
            </button>  
            <SweetAlert2 {...swalProps}>
                <h1>
                    Hello World!
                </h1>
            </SweetAlert2>
        </div>
    );
}