> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# Function: createElement()

使用 snapshot 运行时创建 ReactLynx 元素。

## Call Signature

> **createElement**\<`Type` _extends_ keyof `IntrinsicElements`>(`type`: `Type`, `props?`: `IntrinsicElements`\[`Type`] | `null`, ...`children`: `ReactNode`\[]): `ReactElement`\<`IntrinsicElements`\[`Type`], `Type`>

创建一个 Lynx 内置元素。

### Type Parameters

| Type Parameter                             |
| ------------------------------------------ |
| `Type` _extends_ keyof `IntrinsicElements` |

### Parameters

| Parameter      | Type                                   |
| -------------- | -------------------------------------- |
| `type`         | `Type`                                 |
| `props?`       | `IntrinsicElements`\[`Type`] \| `null` |
| ...`children?` | `ReactNode`\[]                         |

### Returns

`ReactElement`\<`IntrinsicElements`\[`Type`], `Type`>

## Call Signature

> **createElement**\<`Props` _extends_ `object`>(`type`: `string` | `ComponentType`\<`Props`>, `props?`: `Attributes` & `Props` | `null`, ...`children`: `ReactNode`\[]): `ReactElement`\<`Props`>

创建一个组件元素。

### Type Parameters

| Type Parameter             |
| -------------------------- |
| `Props` _extends_ `object` |

### Parameters

| Parameter      | Type                                  |
| -------------- | ------------------------------------- |
| `type`         | `string` \| `ComponentType`\<`Props`> |
| `props?`       | `Attributes` & `Props` \| `null`      |
| ...`children?` | `ReactNode`\[]                        |

### Returns

`ReactElement`\<`Props`>

## 示例

**下面是一个示例: react-apis**

**Entry:** `src/create-element`
**Bundle:** `dist/create-element.lynx.bundle` | Web: `dist/create-element.web.bundle`

```tsx
import { createElement, root, useState } from "@lynx-js/react";

import "./index.css";

interface GreetingProps {
  name: string;
}

function Greeting({ name }: GreetingProps) {
  return createElement(
    "view",
    { className: "Greeting" },
    createElement(
      "text",
      { className: "GreetingText" },
      "Hello ",
      createElement("text", { className: "Name" }, name),
      " ! ",
    ),
    createElement("text", { className: "GreetingText" }, "Welcome !"),
  );
}

function CreateElementExample() {
  const [visible, setVisible] = useState(true);

  const toggleGreeting = () => {
    setVisible((current) => !current);
  };

  return createElement(
    "view",
    { className: "App" },
    createElement(
      "view",
      { className: "Button", bindtap: toggleGreeting },
      createElement(
        "text",
        { className: "ButtonText" },
        visible ? "Hide greeting" : "Show greeting",
      ),
    ),
    visible ? createElement(Greeting, { name: "Lynx" }) : null,
  );
}

root.render(createElement(CreateElementExample, null));

```

v0.2.0


