Preact
Preact 的 API 與 React 相容。整合遵循相同的模式 — 在應用程式啟動時呼叫一次 load(),然後在 JSX 中使用 <wink-*> 標籤。
@wink/elements npm package 安裝並載入帶有 TypeScript 型別的 CDN 套件。
npm install @wink/elements在應用程式啟動時載入一次
Section titled “在應用程式啟動時載入一次”import { useEffect } from 'preact/hooks';import { load } from '@wink/elements';
export function App() { useEffect(() => { load({ clientId: import.meta.env.VITE_WINK_CLIENT_ID }); }, []);
return <YourRoutes />;}在 JSX 中使用
Section titled “在 JSX 中使用”export function HotelsPage() { return ( <main> <wink-content-loader layout="HOTEL" id="YOUR_LAYOUT_ID" /> <wink-lookup /> </main> );}TypeScript — 宣告 JSX 內建元素
Section titled “TypeScript — 宣告 JSX 內建元素”Preact 的 JSX 命名空間與 React 不同。擴充 preact/src/jsx.d.ts:
import type { WinkContentLoaderAttributes, WinkLookupAttributes, WinkSearchButtonAttributes, WinkAccountButtonAttributes, WinkItineraryButtonAttributes, WinkShoppingCartButtonAttributes, WinkAppLoaderAttributes,} from '@wink/elements';
declare module 'preact' { namespace JSX { interface IntrinsicElements { 'wink-content-loader': WinkContentLoaderAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-lookup': WinkLookupAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-search-button': WinkSearchButtonAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-account-button': WinkAccountButtonAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-itinerary-button': WinkItineraryButtonAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-shopping-cart-button': WinkShoppingCartButtonAttributes & preact.JSX.HTMLAttributes<HTMLElement>; 'wink-app-loader': WinkAppLoaderAttributes & preact.JSX.HTMLAttributes<HTMLElement>; } }}使用 preact/compat 的 Preact
Section titled “使用 preact/compat 的 Preact”如果你將 React 別名為 preact/compat,請改用 React 的 JSX 宣告:
// 使用 /integrations/react 中的 React JSX 宣告// — preact/compat 會重新匯出 React 的 JSX 命名空間