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 命名空间