react-toastify icon indicating copy to clipboard operation
react-toastify copied to clipboard

Toast immediately disappears after update of promise toast

Open Drezir opened this issue 2 years ago • 8 comments

I have this toast:

toast.promise(
    resolveAfter3Sec,
    {
      pending: 'Promise is pending',
      success: 'Promise resolved 👌',
    },
    {toastId}
)

My axios layer (in interceptor) is updating toast by its id in case of error:

if (toastId && toast.isActive(toastId)) {
        update(toastId, {
            type: type,
            render: reasonTranslation,
            isLoading: false,
            autoClose: 5000,
        });
    } else {
        toast(reasonTranslation, {
            type: type,
            autoClose: 5000,
            isLoading: false,
            toastId: toastId ?? `${error.code}/${messageKey}`,
        });
    }

If error happens, toast is updated but immediately hidden.

If I stop the execution in this point for a while, it works as expected image

Drezir avatar Jan 25 '23 09:01 Drezir

Hey, can you share a code sandbox with the issue please. Thanks

fkhadra avatar Mar 10 '23 04:03 fkhadra

I have an example here


    const toastId = toast(`Start`);

    toast.update(toastId, {
      render: "This instantly closes the toast :(",
      position: "top-right",
      autoClose: false,
      hideProgressBar: false,
      closeOnClick: true,
      pauseOnHover: true,
      draggable: false,
      progress: 1,
      theme: "light",
    });

Seems like updating the toast with progress 1 always closes it and disregards the "autoClose" property

spectre-akapelon avatar Mar 29 '23 03:03 spectre-akapelon

@spectre-akapelon when you set progress it becomes controlled so autoClose has no effect there

fkhadra avatar Mar 29 '23 14:03 fkhadra

Yes... but why?

The scenario of uploading a file 0% 33% 66% 100% and then letting the user see the toast stay there for a couple of seconds (whatever the autoClose is) is perfectly valid.

I believe the shown progress controlled by the developer and the internal progress i.e. when the toast closes, should be two completely separate things. I don't see why they are connected.

spectre-akapelon avatar Mar 29 '23 14:03 spectre-akapelon

In your case all you have to do is to update the toast each time there is some progress.

progress: 0 progress: 0.33 Till 1

fkhadra avatar Mar 29 '23 14:03 fkhadra

Yes and then it goes away instantly while i don't want it to I want the toast to stay there. Maybe for a predefined time span, maybe until the user closes it. But i certainly don't want to ignore my autoClose:false just because the progress is 1

spectre-akapelon avatar Mar 29 '23 14:03 spectre-akapelon

Ok got your use case. What you can do when you reach 100%. Update the toast with progress null and autoclose with the desired value

fkhadra avatar Mar 29 '23 15:03 fkhadra

Yes, this is kiiiiind of what i'm doing now. I'm setting the value to Math.min(progress, 1 - Number.EPSILON) so that it never reaches 1 and then do my 'autoClose'

spectre-akapelon avatar Mar 29 '23 15:03 spectre-akapelon