这里如何实现toastify?

问题描述 投票:0回答:1

我想在我的

toBeCalledByEventListener()
函数中实现 toastify 。

实际上我不知道在我的函数中在哪里实现这些:

import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

const notify = () => toast("Wow so easy!");

<button onClick={notify}>Notify!</button>

如有任何建议,请。

我的函数如下所示:我想在这里实现 toastify

async function toBeCalledByEventListener() {
    try {
        const response = await fetch('/generate_pdf', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        const responseData = await response.json();
        const pdfURL = responseData.pdf_url;
        localStorage.setItem("pdf_url", pdfURL)
        // window.open(pdfURL, '_blank');
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
    }
    catch (error) {
        console.error('Error fetching data:', error);
    } 
};
javascript reactjs
1个回答
0
投票

你只需要在需要的地方调用toast函数即可。

import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

const notify = () => toast("Wow so easy!");

async function toBeCalledByEventListener() {
   ...
     // if the response was successful call the notification function
     const responseData = await response.json();
     const pdfURL = responseData.pdf_url;
     localStorage.setItem("pdf_url", pdfURL)
     // window.open(pdfURL, '_blank');
     notify()
     if (!response.ok) {
       toast("An unexpected error occurred, please try again later.")
       throw new Error('Network response was not ok');
     }


   ...
}

© www.soinside.com 2019 - 2024. All rights reserved.