DEV Community

taiseen
taiseen

Posted on • Edited on

3

custom cancel button call inside react-toastify

At development time code context/pattern is going to different for project to project... so bellow coding pattern is one of them...

Custom button component call inside "react-toastify"

import ApiCancelBtn from '../components/ApiCancelBtn';
import { toast } from 'react-toastify';

export class Service {
    ...     
    async handleApiCall(callerId) {
        ...
        toast.info(`🙂 We are thinking...`, {
          icon: false,
          autoClose: 30000,
          pauseOnHover: false,
          // custom button component call...
          closeButton: <ApiCancelBtn taskId={'taskId'} />,
        });
        ...
    }
    ...
}
Enter fullscreen mode Exit fullscreen mode

import { toast } from 'react-toastify';
import api from '../api';

const ApiCancelBtn = ({ taskId }) => {

  const handleTaskCancel = async (taskId) => {
    if (taskId !== undefined) {
      await api.ai.cancelTask(taskId);
      toast.dismiss();
    }
  };

  return (
    <button 
       className="toast-custom-cancel-button" 
       onClick={()=> handleTaskCancel(taskId)}
    >
      Cancel API Call
    </button>
  );
};

export default ApiCancelBtn;
Enter fullscreen mode Exit fullscreen mode

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay