sweetalert2 / sweetalert2-react-content

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

React element not working for html property #239

Closed hhoangg closed 1 year ago

hhoangg commented 1 year ago
const confirm = (option: SweetAlertOptions<void, any>, action: () => void) => {
    return MySwal.fire({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonText: 'Yes, delete it!',
      showLoaderOnConfirm: true,
      allowOutsideClick: false,
      preConfirm: action,
      ...option,
    }).then((result) => {
      if (result.isConfirmed) {
        MySwal.fire('Deleted!', 'Your file has been deleted.', 'success')
      }
    })
  }

I created a function like above, but when I pass the property html in it doesn't work properly, instead it shows as [object Object]

image

can you tell me where i am wrong? please.

zenflow commented 1 year ago

@hhoangg Not enough info here. This doesn't tell me what you're passing for html.

Please fork this sandbox to provide a (minimal) reproduction: https://codesandbox.io/s/sweetalert2-react-content-sandbox-6mri0o I'll see if I can help.

As you can see in the sandbox though, the html property seems to be working alright.

hhoangg commented 1 year ago

I'm sorry, my mistake sandbox here

The source on the Codesandbox is perfect work, but the source on my local is not working

Here is the error message

image
zenflow commented 1 year ago

@hhoangg You're using the wrong type for options. Import ReactSweetAlertOptions from "sweetalert2-react-content" and use that instead. The type error is a different issue though since types don't affect runtime behavior.

As you said, the sandbox doesn't reproduce your original issue. I can't really help with that issue unless we have a reproduction. I suggest work on reproducing the issue, by adding to the sandbox step by step, until one step brings out the issue, and then you will probably know what your issue is and you probably won't need any help

hhoangg commented 1 year ago

Import ReactSweetAlertOptions from "sweetalert2-react-content" and use that instead.

This change solved my problem. But I wonder why on Codesandbox I don't use that type but don't get errors like below my source. But anyway this answer solved my problem, thank you for taking some time to help me