錯誤處理與除錯
在整合並使用 TacoTranslate 時,你可能會遇到問題。重要的是要注意,當發生錯誤時,TacoTranslate 的預設行為是僅顯示原始文字。不會拋出錯誤或以其他方式中斷你的應用程式。
不過通常這些問題都很容易解決。以下是一些有助於除錯的實用提示:
檢查主控台日誌
TacoTranslate 會在發生錯誤時輸出除錯資訊。
檢查網路請求
以 tacotranslate 篩選請求並檢查它們的輸出。
TacoTranslate 透過 useTacoTranslate hook 提供一個錯誤物件,可協助您處理與偵錯問題。此物件包含有關翻譯過程中發生的任何錯誤的資訊,讓您能在應用程式中做出適當的回應。
import {useTacoTranslate, Translate} from 'tacotranslate/react';
function Page() {
const {error} = useTacoTranslate();
return (
<div>
{error ? <div>Error: {error.message}</div> : null}
<Translate string="Hello, world!" />
</div>
);
}