Skip to content

Commit 157cc79

Browse files
committed
docs(cn): translate legacy/createElement into Chinese
1 parent d0992f5 commit 157cc79

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

src/content/reference/react/createElement.md

+51-51
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: createElement
44

55
<Intro>
66

7-
`createElement` lets you create a React element. It serves as an alternative to writing [JSX.](/learn/writing-markup-with-jsx)
7+
`createElement` 允许你创建一个 React 元素。它可以作为 [JSX](/learn/writing-markup-with-jsx) 的替代方案。
88

99
```js
1010
const element = createElement(type, props, ...children)
@@ -16,11 +16,11 @@ const element = createElement(type, props, ...children)
1616

1717
---
1818

19-
## Reference {/*reference*/}
19+
## 参考 {/*reference*/}
2020

2121
### `createElement(type, props, ...children)` {/*createelement*/}
2222

23-
Call `createElement` to create a React element with the given `type`, `props`, and `children`.
23+
调用 `createElement` 来创建一个 React 元素,它有 `type``props` `children` 三个参数。
2424

2525
```js
2626
import { createElement } from 'react';
@@ -29,93 +29,93 @@ function Greeting({ name }) {
2929
return createElement(
3030
'h1',
3131
{ className: 'greeting' },
32-
'Hello'
32+
'你好'
3333
);
3434
}
3535
```
3636

37-
[See more examples below.](#usage)
37+
[查看更多例子](#usage)
3838

39-
#### Parameters {/*parameters*/}
39+
#### 参数 {/*parameters*/}
4040

41-
* `type`: The `type` argument must be a valid React component type. For example, it could be a tag name string (such as `'div'` or `'span'`), or a React component (a function, a class, or a special component like [`Fragment`](/reference/react/Fragment)).
41+
* `type``type` 参数必须是一个有效的 React 组件类型,例如一个字符串标签名(如 `'div'` `'span'`),或一个 React 组件(一个函数式组件、一个类式组件,或者是一个特殊的组件如 [`Fragment`](/reference/react/Fragment))。
4242

43-
* `props`: The `props` argument must either be an object or `null`. If you pass `null`, it will be treated the same as an empty object. React will create an element with props matching the `props` you have passed. Note that `ref` and `key` from your `props` object are special and will *not* be available as `element.props.ref` and `element.props.key` on the returned `element`. They will be available as `element.ref` and `element.key`.
43+
* `props``props` 参数必须是一个对象或 `null`。如果你传入 `null`,它会被当作一个空对象。创建的 React 元素的 `props` 与这个参数相同。注意,`props` 对象中的 `ref` `key` 比较特殊,它们 **不会** 作为 `element.props.ref` `element.props.key` 出现在创建的元素 `element` 上,而是作为 `element.ref` `element.key` 出现。
4444

45-
* **optional** `...children`: Zero or more child nodes. They can be any React nodes, including React elements, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), and arrays of React nodes.
45+
* **可选** `...children`:零个或多个子节点。它们可以是任何 React 节点,包括 React 元素、字符串、数字、[portal](/reference/react-dom/createPortal)、空节点(`null``undefined``true` `false`),以及 React 节点数组。
4646

47-
#### Returns {/*returns*/}
47+
#### 返回值 {/*returns*/}
4848

49-
`createElement` returns a React element object with a few properties:
49+
`createElement` 返回一个 React 元素,它有这些属性:
5050

51-
* `type`: The `type` you have passed.
52-
* `props`: The `props` you have passed except for `ref` and `key`. If the `type` is a component with legacy `type.defaultProps`, then any missing or undefined `props` will get the values from `type.defaultProps`.
53-
* `ref`: The `ref` you have passed. If missing, `null`.
54-
* `key`: The `key` you have passed, coerced to a string. If missing, `null`.
51+
* `type`:你传入的 `type`
52+
* `props`:你传入的 `props`,不包括 `ref` `key`。如果 `type` 是一个组件,且带有过时的 `type.defaultProps` 属性,那么 `props` 中任何缺失或未定义的字段都会采用 `type.defaultProps` 中的值。
53+
* `ref`:你传入的 `ref`。如果缺失则为 `null`
54+
* `key`:你传入的 `key`,会被强制转换为字符串。如果缺失则为 `null`
5555

56-
Usually, you'll return the element from your component or make it a child of another element. Although you may read the element's properties, it's best to treat every element as opaque after it's created, and only render it.
56+
通常你会在你组件的最后返回这个元素,或者把它作为另一个元素的子元素。虽然你可以读取元素的属性,但你最好把创建的元素作为黑盒,只用于渲染。
5757

58-
#### Caveats {/*caveats*/}
58+
#### 注意事项 {/*caveats*/}
5959

60-
* You must **treat React elements and their props as [immutable](https://en.wikipedia.org/wiki/Immutable_object)** and never change their contents after creation. In development, React will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) the returned element and its `props` property shallowly to enforce this.
60+
* 你必须 ** React 元素和它们的 props 视为 [不可变的](https://en.wikipedia.org/wiki/Immutable_object)**,在创建后永远不要改变它们的内容。在开发环境中,React 会浅层 [冻结](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) 返回的元素及其 `props` 属性,以确保如此。
6161

62-
* When you use JSX, **you must start a tag with a capital letter to render your own custom component.** In other words, `<Something />` is equivalent to `createElement(Something)`, but `<something />` (lowercase) is equivalent to `createElement('something')` (note it's a string, so it will be treated as a built-in HTML tag).
62+
* 当你使用 JSX 时,**你必须以大写字母开头来渲染你的自定义组件**。换句话说,`<Something />` 等价于 `createElement(Something)`,但 `<something />`(小写)等价于 `createElement('something')`(注意它是一个字符串,它会被当作内置的 HTML 标签)。
6363

64-
* You should only **pass children as multiple arguments to `createElement` if they are all statically known,** like `createElement('h1', {}, child1, child2, child3)`. If your children are dynamic, pass the entire array as the third argument: `createElement('ul', {}, listItems)`. This ensures that React will [warn you about missing `key`s](/learn/rendering-lists#keeping-list-items-in-order-with-key) for any dynamic lists. For static lists this is not necessary because they never reorder.
64+
* 你应该仅 **在所有子元素都是静态可知的情况下,才将它们依次传递给 `createElement` 的可选参数**,比如 `createElement('h1', {}, child1, child2, child3)`。如果你的子元素不固定,则把它们放到数组中作为第三个参数传递,例如 `createElement('ul', {}, listItems)`,以此确保 React 可以在动态列表的场景下 [警告你缺少 `key`](/learn/rendering-lists#keeping-list-items-in-order-with-key)。静态列表的场景不需要这么做,因为它们不会重新排序。
6565

6666
---
6767

68-
## Usage {/*usage*/}
68+
## 用法 {/*usage*/}
6969

70-
### Creating an element without JSX {/*creating-an-element-without-jsx*/}
70+
## 不使用 JSX 创建元素 {/*creating-an-element-without-jsx*/}
7171

72-
If you don't like [JSX](/learn/writing-markup-with-jsx) or can't use it in your project, you can use `createElement` as an alternative.
72+
如果你不喜欢 [JSX](/learn/writing-markup-with-jsx) 或者无法在你的项目中使用它,你可以使用 `createElement` 作为替代方案。
7373

74-
To create an element without JSX, call `createElement` with some <CodeStep step={1}>type</CodeStep>, <CodeStep step={2}>props</CodeStep>, and <CodeStep step={3}>children</CodeStep>:
74+
要想不使用 JSX 创建一个元素,你可以调用 `createElement` 并传入 <CodeStep step={1}>type</CodeStep><CodeStep step={2}>props</CodeStep> <CodeStep step={3}>children</CodeStep>
7575

76-
```js [[1, 5, "'h1'"], [2, 6, "{ className: 'greeting' }"], [3, 7, "'Hello ',"], [3, 8, "createElement('i', null, name),"], [3, 9, "'. Welcome!'"]]
76+
```js [[1, 5, "'h1'"], [2, 6, "{ className: 'greeting' }"], [3, 7, "'你好',"], [3, 8, "createElement('i', null, name),"], [3, 9, "'。欢迎!'"]]
7777
import { createElement } from 'react';
7878

7979
function Greeting({ name }) {
8080
return createElement(
8181
'h1',
8282
{ className: 'greeting' },
83-
'Hello ',
83+
'你好',
8484
createElement('i', null, name),
85-
'. Welcome!'
85+
'。欢迎!'
8686
);
8787
}
8888
```
8989

90-
The <CodeStep step={3}>children</CodeStep> are optional, and you can pass as many as you need (the example above has three children). This code will display a `<h1>` header with a greeting. For comparison, here is the same example rewritten with JSX:
90+
<CodeStep step={3}>children</CodeStep> 是可选的,你可以传入任意数量的子元素(上面的例子中有三个)。这段代码会显示一个带有问候语的 `<h1>` 标题。为了对比,这是使用 JSX 的版本:
9191

92-
```js [[1, 3, "h1"], [2, 3, "className=\\"greeting\\""], [3, 4, "Hello <i>{name}</i>. Welcome!"], [1, 5, "h1"]]
92+
```js [[1, 3, "h1"], [2, 3, "className=\\"greeting\\""], [3, 4, "你好<i>{name}</i>,欢迎!"], [1, 5, "h1"]]
9393
function Greeting({ name }) {
9494
return (
9595
<h1 className="greeting">
96-
Hello <i>{name}</i>. Welcome!
96+
你好<i>{name}</i>,欢迎!
9797
</h1>
9898
);
9999
}
100100
```
101101

102-
To render your own React component, pass a function like `Greeting` as the <CodeStep step={1}>type</CodeStep> instead of a string like `'h1'`:
102+
要想渲染你自己的 React 组件,则传入一个函数(比如 `Greeting`)作为 <CodeStep step={1}>type</CodeStep> ,而不是一个字符串(比如 `'h1'`):
103103

104-
```js [[1, 2, "Greeting"], [2, 2, "{ name: 'Taylor' }"]]
104+
```js [[1, 2, "Greeting"], [2, 2, "{ name: '泰勒' }"]]
105105
export default function App() {
106-
return createElement(Greeting, { name: 'Taylor' });
106+
return createElement(Greeting, { name: '泰勒' });
107107
}
108108
```
109109

110-
With JSX, it would look like this:
110+
如果使用 JSX,它看起来像这样:
111111

112-
```js [[1, 2, "Greeting"], [2, 2, "name=\\"Taylor\\""]]
112+
```js [[1, 2, "Greeting"], [2, 2, "name=\\"泰勒\\""]]
113113
export default function App() {
114-
return <Greeting name="Taylor" />;
114+
return <Greeting name="泰勒" />;
115115
}
116116
```
117117

118-
Here is a complete example written with `createElement`:
118+
这里是一个完整的使用 `createElement` 的示例:
119119

120120
<Sandpack>
121121

@@ -126,16 +126,16 @@ function Greeting({ name }) {
126126
return createElement(
127127
'h1',
128128
{ className: 'greeting' },
129-
'Hello ',
129+
'你好',
130130
createElement('i', null, name),
131-
'. Welcome!'
131+
',欢迎!'
132132
);
133133
}
134134

135135
export default function App() {
136136
return createElement(
137137
Greeting,
138-
{ name: 'Taylor' }
138+
{ name: '泰勒' }
139139
);
140140
}
141141
```
@@ -149,21 +149,21 @@ export default function App() {
149149

150150
</Sandpack>
151151

152-
And here is the same example written using JSX:
152+
这里是相同的示例,但使用的是 JSX
153153

154154
<Sandpack>
155155

156156
```js
157157
function Greeting({ name }) {
158158
return (
159159
<h1 className="greeting">
160-
Hello <i>{name}</i>. Welcome!
160+
你好<i>{name}</i>,欢迎!
161161
</h1>
162162
);
163163
}
164164

165165
export default function App() {
166-
return <Greeting name="Taylor" />;
166+
return <Greeting name="泰勒" />;
167167
}
168168
```
169169

@@ -176,30 +176,30 @@ export default function App() {
176176

177177
</Sandpack>
178178

179-
Both coding styles are fine, so you can use whichever one you prefer for your project. The main benefit of using JSX compared to `createElement` is that it's easy to see which closing tag corresponds to which opening tag.
179+
两种编码风格都没问题,你可以在项目中使用任何一个你喜欢的风格。相比于 `createElement`,使用 JSX 的主要好处是很容易看出哪个闭合标签对应哪个开放标签。
180180

181181
<DeepDive>
182182

183-
#### What is a React element, exactly? {/*what-is-a-react-element-exactly*/}
183+
#### React 元素究竟是什么? {/*what-is-a-react-element-exactly*/}
184184

185-
An element is a lightweight description of a piece of the user interface. For example, both `<Greeting name="Taylor" />` and `createElement(Greeting, { name: 'Taylor' })` produce an object like this:
185+
元素是用来描述一部分用户界面的轻量级结构。比如,`<Greeting name="泰勒" />` `createElement(Greeting, { name: '泰勒' })` 都会生成一个这样的对象:
186186

187187
```js
188-
// Slightly simplified
188+
// 极度简化的样子
189189
{
190190
type: Greeting,
191191
props: {
192-
name: 'Taylor'
192+
name: '泰勒'
193193
},
194194
key: null,
195195
ref: null,
196196
}
197197
```
198198

199-
**Note that creating this object does not render the `Greeting` component or create any DOM elements.**
199+
**注意,创建这个对象并不会渲染 `Greeting` 组件或者创建任何 DOM 元素**
200200

201-
A React element is more like a description--an instruction for React to later render the `Greeting` component. By returning this object from your `App` component, you tell React what to do next.
201+
React 元素更像是一个描述或指令,它告诉 React 之后该如何渲染 `Greeting` 组件。你从 `App` 组件中返回了这个对象,就是告诉了 React 接下来该做什么。
202202

203-
Creating elements is extremely cheap so you don't need to try to optimize or avoid it.
203+
创建元素非常高效,因此你不需要试图优化或者避免它。
204204

205205
</DeepDive>

0 commit comments

Comments
 (0)