> ## Documentation Index
> Fetch the complete documentation index at: https://docs.li.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# 与 Next.js、Remix、Nuxt 等框架的兼容性

> 将 widget 集成到你的 Next.js、Remix、Nuxt 或其他框架应用中

## Next.js

**LI.FI Widget** 与 Next.js 应用完全兼容，只需极少的配置即可无缝集成。

由于 Next.js 中服务端渲染（SSR）的特性，以及不同钱包库管理其与钱包扩展连接的方式，LI.FI Widget 需要专门在客户端进行渲染。为此，请使用 `'use client'` 指令，确保 widget 组件仅在客户端环境中渲染。

请查看我们在 widget 仓库中的完整示例[这里](https://github.com/lifinance/widget/tree/main/examples)。

如果你正在使用带 App Router 的 Next.js，以下示例展示了如何将 widget 添加到某个页面：

```typescript Widget.tsx theme={"system"}
'use client';

import type { WidgetConfig } from '@lifi/widget';
import { LiFiWidget, WidgetSkeleton } from '@lifi/widget';
import { ClientOnly } from './ClientOnly';

export function Widget() {
  const config = {
    appearance: 'light',
    theme: {
      container: {
        boxShadow: '0px 8px 32px rgba(0, 0, 0, 0.08)',
        borderRadius: '16px',
      },
    },
  } as Partial<WidgetConfig>;

  return (
    <div>
      <ClientOnly fallback={<WidgetSkeleton config={config} />}>
        <LiFiWidget config={config} integrator="nextjs-example" />
      </ClientOnly>
    </div>
  );
}
```

```typescript ClientOnly.tsx theme={"system"}
import { useSyncExternalStore, type PropsWithChildren } from 'react';

function subscribe() {
  return () => {};
}

/**
 * Return a boolean indicating if the JS has been hydrated already.
 * When doing Server-Side Rendering, the result will always be false.
 * When doing Client-Side Rendering, the result will always be false on the
 * first render and true from then on. Even if a new component renders it will
 * always start with true.
 */
export function useHydrated() {
  return useSyncExternalStore(
    subscribe,
    () => true,
    () => false,
  );
}

interface ClientOnlyProps extends PropsWithChildren {
  fallback?: React.ReactNode;
}

/**
 * Render the children only after the JS has loaded client-side. Use an optional
 * fallback component if the JS is not yet loaded.
 */
export function ClientOnly({ children, fallback = null }: ClientOnlyProps) {
  const hydrated = useHydrated();
  return hydrated ? children : fallback;
}
```

```typescript page.tsx theme={"system"}
import { Widget } from './components/Widget';

export default function Home() {
  return (
    <main>
      <Widget />
    </main>
  );
}
```

如果你正在使用带 Pages Router 的 Next.js，则需要动态导入 LI.FI Widget：

```tsx theme={"system"}
import dynamic from 'next/dynamic';

const LiFiWidget = dynamic(() => import('@lifi/widget').then(mod => mod.LiFiWidget), {
  ssr: false,
  loading: () => <div>Loading LI.FI Widget...</div>
});

export function Widget() {
  const config = {
    appearance: 'light',
    theme: {
      container: {
        boxShadow: '0px 8px 32px rgba(0, 0, 0, 0.08)',
        borderRadius: '16px',
      },
    },
  } as Partial<WidgetConfig>;

  return (
    <LiFiWidget config={config} integrator="nextjs-example" />
  );
}
```

## Widget 骨架屏

为方便用户，我们提供了 `WidgetSkeleton` 组件，可在主 widget 组件加载期间用作占位内容。

> [!NOTE]
>
> `WidgetSkeleton` 组件目前仅在 Next.js 环境中经过测试，可能无法与其他 SSR 框架配合使用。

![](https://mintcdn.com/lifi/EmProAEgMrruLUbI/images/widget_skeleton.gif?s=6eac36570e37135ef5108b35a3bcff7e)

Widget 骨架屏

## 内部路由

Widget v4 在内部使用 TanStack Router，而不再使用 React Router。这意味着：

* **无需通配符路由**：如果你之前在使用 React Router 时必须为 widget 配置通配符路由（例如 `/swap/*`），现在不再需要这样做。
* **无冲突**：widget 的内部路由完全隔离，不会干扰你应用的路由，无论你使用哪个路由库。

## 其他框架

我们为各种框架提供了完整示例：

* [Vite (React)](https://github.com/lifinance/widget/tree/main/examples/vite) - 标准 React 设置，无需特殊配置
* [Remix](https://github.com/lifinance/widget/tree/main/examples/remix) - 使用 `remix-utils` 中的 `ClientOnly`
* [React Router 7](https://github.com/lifinance/widget/tree/main/examples/react-router-7) - 采用与 `ClientOnly` 类似的模式
* [TanStack Router](https://github.com/lifinance/widget/tree/main/examples/tanstack-router) - 可与 widget 的内部路由并存
* [Vue](https://github.com/lifinance/widget/tree/main/examples/vue) - 使用 [Veaury](https://github.com/devilwjp/veaury) 包装 React 组件
* [Nuxt.js](https://github.com/lifinance/widget/tree/main/examples/nuxt) - 将 Veaury 与 Nuxt 的 `ClientOnly` 配合使用
* [Svelte](https://github.com/lifinance/widget/tree/main/examples/svelte) - React 适配器组件

## 钱包提供者集成

我们还提供了将 widget 与流行钱包连接库集成的示例：

* [RainbowKit](https://github.com/lifinance/widget/tree/main/examples/rainbowkit) - 面向 Ethereum 的流行钱包连接库
* [ConnectKit](https://github.com/lifinance/widget/tree/main/examples/connectkit) - Family 的钱包连接库
* [Reown AppKit](https://github.com/lifinance/widget/tree/main/examples/reown) - Reown（前身为 WalletConnect）的 AppKit
* [Privy](https://github.com/lifinance/widget/tree/main/examples/privy) - Privy 内嵌钱包集成
* [Privy with Ethers](https://github.com/lifinance/widget/tree/main/examples/privy-ethers) - Privy 与 Ethers.js
* [Dynamic](https://github.com/lifinance/widget/tree/main/examples/dynamic) - Dynamic 钱包集成

## 其他示例

* [Zustand Widget Config](https://github.com/lifinance/widget/tree/main/examples/zustand-widget-config) - 使用 Zustand 进行状态管理
* [Deposit Flow](https://github.com/lifinance/widget/tree/main/examples/deposit-flow) - 自定义存款流程实现

我们正在持续改进对更多框架的支持。请在[示例目录](https://github.com/lifinance/widget/tree/main/examples)中查看所有可用示例。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.