在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
在現代網頁開發中,提供及時的反饋給用戶對於流暢的用戶體驗至關重要。 Toast 通知是一種有效的訊息傳遞方式,能在不打擾使用者工作流程的情況下提供訊息。 這React-toastify該程式包因其簡單性和靈活性而成為在 React 應用程式中實現 toast 通知的熱門選擇。 我們也將調查IronPDFNPM 套件用於生成、編輯和管理 PDF 文件。 本文將引導您完成整合的過程React-toastify和IronPDF將其引入到您的 React 專案中。
React-toastify是一个 NPM 包,允许您将可自定义的提示通知添加到您的 React 应用程序中,且设置非常简便。它提供多种功能,包括不同的通知类型、自动关闭功能、自定义样式、剩余时间显示等。
要開始使用react-toastify,您需要透過 NPM 或 Yarn 安裝套件。 在您的專案根目錄中執行以下命令:
npm install react-toastify
npm install react-toastify
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npm install react-toastify
或
yarn add react-toastify
yarn add react-toastify
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'yarn add react-toastify
安裝套件後,您可以在您的 React 應用程式中開始使用 react-toastify。 以下是一個簡單的代碼示例,展示如何整合和使用 react-toastify。
首先,您需要從 react-toastify 導入必要的組件:
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import React from 'react'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css';
接下來,將 ToastContainer 元件添加到您的應用程式中。
function App() {
return (
<div> // react component rendered inside
<ToastContainer />
</div>
);
}
function App() {
return (
<div> // react component rendered inside
<ToastContainer />
</div>
);
}
Private Function App() As [function]
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Return ((Of div) <ToastContainer /> </div>)
End Function
您可以使用 toast 函數觸發通知提示。 以下是顯示成功消息的程式碼範例:
function notify() {
toast.success("Success! This is a success message.", {
position: toast.POSITION.TOP_RIGHT
});
}
function App() {
return (
<div>
<button onClick={notify}>Show Toast</button>
<ToastContainer /> // react component inside
</div>
);
}
function notify() {
toast.success("Success! This is a success message.", {
position: toast.POSITION.TOP_RIGHT
});
}
function App() {
return (
<div>
<button onClick={notify}>Show Toast</button>
<ToastContainer /> // react component inside
</div>
);
}
Private Function notify() As [function]
toast.success("Success! This is a success message.", { position:= toast.POSITION.TOP_RIGHT })
End Function
Private Function App() As [function]
'INSTANT VB TODO TASK: The following line contains an assignment within expression that was not extracted by Instant VB:
'ORIGINAL LINE: return (<div> <button onClick={notify}> Show Toast</button> <ToastContainer /> </div>);
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Return ((Of div) <button onClick={AddressOf notify}> Show Toast</button> <ToastContainer /> </div>)
End Function
React-toastify 提供多種進階功能,讓您可以使用 onOpen 和 onClose 鉤子來自訂吐司的行為和外觀。
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App() {
const notify = () => {
toast("Hello there", {
onOpen: () => window.alert('Called when I open'),
onClose: () => window.alert('Called when I close')
});
};
return (
<div>
<button onClick={notify}>Notify</button>
<ToastContainer />
</div>
);
}
export default App;
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App() {
const notify = () => {
toast("Hello there", {
onOpen: () => window.alert('Called when I open'),
onClose: () => window.alert('Called when I close')
});
};
return (
<div>
<button onClick={notify}>Notify</button>
<ToastContainer />
</div>
);
}
export default App;
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import React from 'react'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; @function App() { const notify = () => { toast("Hello there", { onOpen: () => window.alert('Called @when I open'), onClose: () => window.alert('Called @when I close') }); }; Return(<div> <button onClick={notify}> Notify</button> <ToastContainer /> </div>); } export default App;
在此範例中:
您可以使用 position 選項在螢幕上不同位置顯示提示框:
toast.info("Information message", {
position:"top-right"
});
toast.info("Information message", {
position:"top-right"
});
toast.info("Information message", { position:="top-right" })
您可以使用 autoClose 選項設置吐司顯示的持續時間:
toast.warn("Warning message", {
autoClose: 5000 // Auto close after 5 seconds
});
toast.warn("Warning message", {
autoClose: 5000 // Auto close after 5 seconds
});
toast.warn("Warning message", { autoClose:= 5000 })
可以使用 className 和 style 選項對提示訊息進行自定義樣式設定:
toast.error("Error message", {
className: 'custom-toast',
style: { background: 'red', color: 'white' }
});
toast.error("Error message", {
className: 'custom-toast',
style: { background: 'red', color: 'white' }
});
toast.error("Error message", { className: 'custom-toast', style: { background: 'red', color: 'white' } });
可以使用 toast.dismiss 方法以程式方式關閉通知:
const toastId = toast("This toast can be dismissed");
function dismissToast() {
toast.dismiss(toastId);
}
const toastId = toast("This toast can be dismissed");
function dismissToast() {
toast.dismiss(toastId);
}
Private const toastId = toast("This toast can be dismissed")
Private Function dismissToast() As [function]
toast.dismiss(toastId)
End Function
以下是一個完整的範例,展示如何使用各種 react-toastify 的功能:
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App() {
const notify = () => {
toast.success("Success! This is a success message.", {
position:"top-right" // success notification
});
toast.info("Information message", {
position:"bottom-left" // info will be displayed with progress bar
});
toast.warn("Warning message", {
autoClose: 5000 // close or pause toast messages
});
toast.error("Error message", {
className: 'custom-toast',
style: { background: 'red', color: 'white' }
});
};
return (
<div>
<button onClick={notify}>Show Toasts</button>
<ToastContainer />
</div>
);
}
export default App;
import React from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
function App() {
const notify = () => {
toast.success("Success! This is a success message.", {
position:"top-right" // success notification
});
toast.info("Information message", {
position:"bottom-left" // info will be displayed with progress bar
});
toast.warn("Warning message", {
autoClose: 5000 // close or pause toast messages
});
toast.error("Error message", {
className: 'custom-toast',
style: { background: 'red', color: 'white' }
});
};
return (
<div>
<button onClick={notify}>Show Toasts</button>
<ToastContainer />
</div>
);
}
export default App;
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import React from 'react'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; @function App() { const notify = () => { toast.success("Success! This is a success message.", { position:"top-right" }); toast.info("Information message", { position:"bottom-left" }); toast.warn("Warning message", { autoClose: 5000 }); toast.@error("Error message", { className: 'custom-toast', style: { background: 'red', color: 'white' } }); }; Return(<div> <button onClick={notify}> Show Toasts</button> <ToastContainer /> </div>); } export default App;
IronPDF是一個強大的 C# PDF 庫,允許開發人員在他們的 .NET 專案中生成和編輯 PDF。 無論您需要從 HTML 創建 PDF、操作現有的 PDF,還是將網頁轉換為 PDF 格式,IronPDF 都能滿足您的需求。
以下是一些主要功能和使用案例:
IronPDF 可以將 HTML 頁面轉換為 PDF,無論是來自 URL、HTML 文件還是 HTML 字串。 您還可以將本地 HTML 文件或 HTML 字串轉換為 PDF。
IronPDF 可以在各種平台上無縫運行,包括:
IronPDF 允許您:
您可以套用頁面模板、頁首、頁尾、頁碼和自定義邊距。 IronPDF 支援 UTF-8 字元編碼、基礎網址、資源編碼等。
IronPDF 遵循多種 PDF 標準,包括 PDF 版本(1.2 - 1.7), PDF/UA(PDF/UA-1),和 PDF/A(PDF/A-3b).
安裝依賴項:首先,創建一個新的 Next.js 專案(如果您還沒有)使用以下命令:請參考設置頁面。
npx create-next-app@latest my-pdf-app --use-npm --example "https://github.com/vercel/next-learn/tree/main/basics/learn-starter"
npx create-next-app@latest my-pdf-app --use-npm --example "https://github.com/vercel/next-learn/tree/main/basics/learn-starter"
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npx create-@next-app@latest my-pdf-app --use-npm --example "https://github.com/vercel/next-learn/tree/main/basics/learn-starter"
接著,導航至您的專案目錄:
cd my-pdf-app
cd my-pdf-app
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'cd my-pdf-app
安裝所需的軟體包:
npm install @ironsoftware/ironpdf
npm install react-toastify
npm install @ironsoftware/ironpdf
npm install react-toastify
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npm install @ironsoftware/ironpdf npm install react-toastify
創建 PDF:現在,讓我們來創建一個使用生成 PDF 的簡單示例。IronPDF. 在您的 Next.js 組件中(例如,pages/index.tsx),新增以下代碼:
import Head from 'next/head';
import styles from '../styles/Home.module.css';
import {ToastContainer, toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import {useState} from "react";
export default function Home() {
const [textInput, setTextInput] = useState('');
const notify = () => {
toast.success("Success! This is a success message.", {
position: "top-right"
});
toast.info("Information message", {
position: "bottom-left"
});
toast.warn("Warning message", {
autoClose: 5000
});
toast.error("Error message", {
className: 'custom-toast',
style: {background: 'red', color: 'white'}
});
};
const generatePdf = async () => {
try {
const response = await fetch('/api/pdf?url='+textInput);
const blob = await response.blob();
const url = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'example.pdf');
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
} catch (error) {
console.error('Error generating PDF:', error);
}
};
const handleChange = (event) => {
setTextInput(event.target.value);
}
return (
<div className={styles.container}>
<Head>
<title>Demo Toaster and Generate PDF From IronPDF</title>
<link rel="icon" href="/favicon.ico"/>
</Head>
<main>
<h1>Demo Toaster and Generate PDF From IronPDF</h1>
<button style={{margin:20, padding:5}} onClick={notify}>Show Toasts</button>
<p>
<span>Enter Url To Convert to PDF:</span>{" "}
</p>
<button style={{margin:20, padding:5}} onClick={generatePdf}>Generate PDF</button>
<ToastContainer/>
</main>
<style jsx>{`
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
margin-left: 0.5rem;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
text-decoration: none;
color: inherit;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo,
Monaco,
Lucida Console,
Liberation Mono,
DejaVu Sans Mono,
Bitstream Vera Sans Mono,
Courier New,
monospace;
}
`}</style>
<style jsx global>{`
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system,
BlinkMacSystemFont,
Segoe UI,
Roboto,
Oxygen,
Ubuntu,
Cantarell,
Fira Sans,
Droid Sans,
Helvetica Neue,
sans-serif;
}
* {
box-sizing: border-box;
}
`}</style>
</div>
);
}
import Head from 'next/head';
import styles from '../styles/Home.module.css';
import {ToastContainer, toast} from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import {useState} from "react";
export default function Home() {
const [textInput, setTextInput] = useState('');
const notify = () => {
toast.success("Success! This is a success message.", {
position: "top-right"
});
toast.info("Information message", {
position: "bottom-left"
});
toast.warn("Warning message", {
autoClose: 5000
});
toast.error("Error message", {
className: 'custom-toast',
style: {background: 'red', color: 'white'}
});
};
const generatePdf = async () => {
try {
const response = await fetch('/api/pdf?url='+textInput);
const blob = await response.blob();
const url = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'example.pdf');
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
} catch (error) {
console.error('Error generating PDF:', error);
}
};
const handleChange = (event) => {
setTextInput(event.target.value);
}
return (
<div className={styles.container}>
<Head>
<title>Demo Toaster and Generate PDF From IronPDF</title>
<link rel="icon" href="/favicon.ico"/>
</Head>
<main>
<h1>Demo Toaster and Generate PDF From IronPDF</h1>
<button style={{margin:20, padding:5}} onClick={notify}>Show Toasts</button>
<p>
<span>Enter Url To Convert to PDF:</span>{" "}
</p>
<button style={{margin:20, padding:5}} onClick={generatePdf}>Generate PDF</button>
<ToastContainer/>
</main>
<style jsx>{`
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer img {
margin-left: 0.5rem;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
text-decoration: none;
color: inherit;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo,
Monaco,
Lucida Console,
Liberation Mono,
DejaVu Sans Mono,
Bitstream Vera Sans Mono,
Courier New,
monospace;
}
`}</style>
<style jsx global>{`
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system,
BlinkMacSystemFont,
Segoe UI,
Roboto,
Oxygen,
Ubuntu,
Cantarell,
Fira Sans,
Droid Sans,
Helvetica Neue,
sans-serif;
}
* {
box-sizing: border-box;
}
`}</style>
</div>
);
}
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'import Head from '@next/head'; import styles from '../styles/Home.@module.css'; import {ToastContainer, toast} from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import {useState} from "react"; export default @function Home() { const [textInput, setTextInput] = useState(''); const notify = () => { toast.success("Success! This is a success message.", { position: "top-right" }); toast.info("Information message", { position: "bottom-left" }); toast.warn("Warning message", { autoClose: 5000 }); toast.@error("Error message", { className: 'custom-toast', style: {background: 'red', color: 'white'} }); }; const generatePdf = async() => { try { const response = await fetch('/api/System.Nullable<pdf>url='+textInput); const blob = await response.blob(); const url = window.URL.createObjectURL(New Blob([blob])); const link = document.createElement("a"c); link.href = url; link.setAttribute('download', 'example.pdf'); document.body.appendChild(link); link.click(); link.parentNode.removeChild(link); } catch(@error) { console.@error('@Error generating PDF:', @error); } }; const handleChange = (event) => { setTextInput(event.target.value); } Return(<div className={styles.container}> <Head> <title> Demo Toaster @and Generate PDF From IronPDF</title> <link rel="icon" href="/favicon.ico"/> </Head> <main> <h1> Demo Toaster @and Generate PDF From IronPDF</h1> <button style={{margin:20, padding:5}} onClick={notify}> Show Toasts</button> <p> <span> Enter Url @To Convert @to PDF:</span>{" "} </p> <button style={{margin:20, padding:5}} onClick={generatePdf}> Generate PDF</button> <ToastContainer/> </main> <style jsx>{` main { padding: 5rem 0; flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; } footer { width: 100%; height: 100px; border-top: 1px solid #eaeaea; display: flex; justify-content: center; align-items: center; } footer img { margin-left: 0.5rem; } footer a { display: flex; justify-content: center; align-items: center; text-decoration: none; color: inherit; } code { background: #fafafa; border-radius: 5px; padding: 0.75rem; font-size: 1.1rem; font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier @New, monospace; } `}</style> <style jsx global>{` html, body { padding: 0; margin: 0; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; } * { box-sizing: border-box; } `}</style> </div>); }
由於 IronPDF 僅在 Node.js 上運行,接下來為應用程式添加一個 API,以便使用 Node.js 生成 PDF。
在 pages/api 資料夾中建立一個名為 pdf.js 的文件,並加入以下代碼。
// pages/api/pdf.js
import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";
export default async function handler(req, res) {
try {
const url = req.query.url
const pdf = await PdfDocument.fromUrl(url);
const data = await pdf.saveAsBuffer();
console.error('data PDF:', data);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=awesomeIron.pdf');
res.send(data);
} catch (error) {
console.error('Error generating PDF:', error);
res.status(500).end();
}
}
// pages/api/pdf.js
import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";
export default async function handler(req, res) {
try {
const url = req.query.url
const pdf = await PdfDocument.fromUrl(url);
const data = await pdf.saveAsBuffer();
console.error('data PDF:', data);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=awesomeIron.pdf');
res.send(data);
} catch (error) {
console.error('Error generating PDF:', error);
res.status(500).end();
}
}
' pages/api/pdf.js
import
If True Then
IronPdfGlobalConfig, PdfDocument
End If
from "@ironsoftware/ironpdf"
' Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here"
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'export default async @function handler(req, res)
'{
' try
' {
' const url = req.query.url const pdf = await PdfDocument.fromUrl(url);
' const data = await pdf.saveAsBuffer();
' console.@error('data PDF:', data);
' res.setHeader('Content-Type', 'application/pdf');
' res.setHeader('Content-Disposition', 'attachment; filename=awesomeIron.pdf');
' res.send(data);
' }
' catch (@error)
' {
' console.@error('@Error generating PDF:', @error);
' res.status(500).@end();
' }
'}
注意:在上述代碼中添加您自己的授權密鑰
運行您的應用程式:啟動您的 Next.js 應用程式:
npm run dev
or
yarn dev
npm run dev
or
yarn dev
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'npm run dev @or yarn dev
打開您的瀏覽器並瀏覽至 http://localhost:3000 以查看以下網站。
現在點擊「顯示提示訊息」按鈕查看提示訊息
現在輸入網站 URL 生成 PDF,然後點擊生成 PDF。 如下所示的名為 awesomeIron.pdf 的檔案將被下載。
IronPDF頁面。
將授權密鑰放置在應用程式中,如下所示:
import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";
import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";
IRON VB CONVERTER ERROR developers@ironsoftware.com
React-toastify是一個功能強大且易於使用的函式庫,用於向您的 React 應用程式添加吐司通知。 透過其廣泛的功能和自訂選項,您可以以超級簡單且不具侵入性的方式提供即時回饋,提高用戶體驗。 另一方面IronPDF是目前最通用的企業級庫,支持生成、編輯和管理 PDF 文件。 按照本文所述的步驟,您可以快速整合React-toastify和IronPDF進入您的專案並開始利用其功能。