> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/reactjs-javascript.md).

# 在 React JavaScript 应用中嵌入体验

在用 JavaScript 编写的 React 应用中安装 Pixel Streaming Embed 包，渲染已发布的体验，并通过你自己的按钮启动它。

在使用 JavaScript 编写的 React 应用中渲染已发布的 Convai Pixel Streaming 体验。此组件及其选项与 TypeScript 中的相同，只是不包含类型注解。最后，用户启动体验后，它会在页面中播放。

### 前提条件

* 使用 Vite 等打包工具构建的 React 18 应用。
* 已发布体验的 ID，以及一种授权方式：用于返回 Convai 身份验证令牌的服务器路由，或已添加到 Convai 允许列表中的域名。请参阅 [发布并授权体验](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/whitelisting-and-publishing-an-experience.md).

### 安装软件包

当前版本为 <code class="expression">space.vars.experience\_embed\_version</code>.

{% tabs %}
{% tab title="npm" %}

```bash
npm install @convai/experience-embed
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm add @convai/experience-embed
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn add @convai/experience-embed
```

{% endtab %}
{% endtabs %}

### 渲染体验

此示例从服务器上的路由获取身份验证令牌， `/api/convai-token`然后使用体验自带的启动按钮进行渲染。 `PixelStreamComponent` 会填满其父元素，因此请为父元素设置尺寸：

{% code title="src/App.jsx" %}

```jsx
import { useEffect, useRef, useState } from 'react';
import { PixelStreamComponent } from '@convai/experience-embed';

export default function App() {
  const streamRef = useRef(null);
  const [token, setToken] = useState();

  useEffect(() => {
    fetch('/api/convai-token')
      .then((response) => response.json())
      .then((body) => setToken(body.token));
  }, []);

  if (!token) return <p>Loading…</p>;

  return (
    <div style={{ width: '100%', height: '100vh' }}>
      <PixelStreamComponent
        ref={streamRef}
        expId="YOUR_EXPERIENCE_ID"
        authToken={token}
        InitialScreen={
          <button onClick={() => streamRef.current?.initializeExperience()}>
            启动体验
          </button>
        }
        onIframeLoaded={() => console.log('Experience loaded')}
      />
    </div>
  );
}
```

{% endcode %}

如果 Convai 已将你的域名加入允许列表，则省略 `authToken` 并直接渲染组件。若不提供 `InitialScreen`，组件会显示 **点击以启动体验** 界面，点击后即可启动体验。

### 控制体验

在 `onIframeLoaded` 触发后，可以调用 ref 上的方法，例如 `streamRef.current?.enableCamera()` 或 `streamRef.current?.sendMessageToCharacter('Hello')`。 [Pixel Streaming Embed API 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/api-reference.md) 列出了所有方法和回调。

### 验证嵌入

加载页面并选择 **启动体验**.

{% hint style="success" %}
体验会替换按钮，浏览器控制台会记录 `Experience loaded`.
{% endhint %}

如果嵌入内容显示错误消息，请参阅 TypeScript 页面中的 [故障排除](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/react-typescript.md#troubleshooting) ；原因相同。

### 后续步骤

{% content-ref url="/pages/87b57936662c69d8802f20f500191b181163cf02" %}
[Pixel Streaming Embed API 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/api-reference.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/reactjs-javascript.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
