Remix
Remix 是一个默认进行服务器渲染的全栈框架。使用根组件中的 useEffect 仅在客户端加载 Wink CDN。
@wink/elements npm package 安装并加载带有 TypeScript 类型的 CDN 包。
npm install @wink/elements在 app/root.tsx 中加载
Section titled “在 app/root.tsx 中加载”useEffect 仅在浏览器中运行,服务器端渲染时不会执行:
import { useEffect } from 'react';import { Links, Meta, Outlet, Scripts, ScrollRestoration,} from '@remix-run/react';import { load } from '@wink/elements';
export default function App() { useEffect(() => { load({ clientId: import.meta.env.VITE_WINK_CLIENT_ID }); }, []);
return ( <html lang="en"> <head> <Meta /> <Links /> </head> <body> <Outlet /> <ScrollRestoration /> <Scripts /> </body> </html> );}Remix(基于 Vite,v2.8+)支持通过 import.meta.env 访问浏览器可见的变量。使用 VITE_ 前缀以暴露给浏览器包:
VITE_WINK_CLIENT_ID=your-client-id对于使用 Node.js 适配器的旧版 Remix 设置,可从根 loader 返回客户端 ID 并在组件中读取:
import { json } from '@remix-run/node';import { useLoaderData } from '@remix-run/react';
export async function loader() { return json({ winkClientId: process.env.WINK_CLIENT_ID });}
export default function App() { const { winkClientId } = useLoaderData<typeof loader>();
useEffect(() => { if (winkClientId) { load({ clientId: winkClientId }); } }, [winkClientId]);
// ...}在路由中使用
Section titled “在路由中使用”export default function HotelsRoute() { return ( <main> <wink-content-loader layout="HOTEL" id="YOUR_LAYOUT_ID" /> </main> );}TypeScript 类型
Section titled “TypeScript 类型”从 React 集成页面 添加 JSX 内置声明文件 — Remix 底层使用 React。