timolins / react-hot-toast

Smoking Hot React Notifications 🔥
https://react-hot-toast.com
MIT License
9.85k stars 331 forks source link

Dynamic icon within successful promise #259

Closed dudo closed 1 year ago

dudo commented 1 year ago

I'm working with an api client that will never reject (URQL). Is there a way to dynamically change the icon (and duration for that matter) within a success response?

toast.promise(myPromise), {
  loading: "Saving...",
  success: ({ error }) => {
    if (error) {
      // I'd like to treat the toast as an error at this point
      console.error({ error });
      return error.message;
    }
    return "Saved!"
  },
  error: (error) => {
    // This will likely never be reached
    console.error({ error });
    return error.message
  }
});
dudo commented 1 year ago

This is what I've come up with so far...

const toastId = toast.loading("Saving...");
myPromise.then(({ error }) => {
  if (error) {
    toast.error(error.message, {
      id: toastId,
    });
  } else {
    toast.success("Saved!", {
      id: toastId,
    });
  }
});

I think this answers my question for now.