ข้ามไปยังเนื้อหา

Preact

API ของ Preact เข้ากันได้กับ React การผสานรวมทำตามรูปแบบเดียวกัน — เรียกใช้ load() ครั้งเดียวตอนเริ่มแอป จากนั้นใช้แท็ก <wink-*> ใน JSX

Terminal window
npm install @wink/elements
src/app.tsx
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 />;
}
src/pages/hotels.tsx
export function HotelsPage() {
return (
<main>
<wink-content-loader layout="HOTEL" id="YOUR_LAYOUT_ID" />
<wink-lookup />
</main>
);
}

namespace JSX ของ Preact แยกจาก React ให้เพิ่มใน preact/src/jsx.d.ts:

src/wink-elements.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>;
}
}
}

ถ้าคุณตั้ง alias React เป็น preact/compat ให้ใช้การประกาศ JSX ของ React แทน:

src/wink-elements.d.ts
// ใช้การประกาศ JSX ของ React จาก /integrations/react
// — preact/compat จะ re-export namespace JSX ของ React