You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`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)).
*`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`.
***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.
`createElement`returns a React element object with a few properties:
49
+
`createElement`返回一个 React 元素,它有这些属性:
50
50
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`.
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.
*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.
*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 标签)。
63
63
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.
To create an element without JSX, call`createElement`with some <CodeStepstep={1}>type</CodeStep>, <CodeStepstep={2}>props</CodeStep>, and <CodeStepstep={3}>children</CodeStep>:
The <CodeStepstep={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:
Here is a complete example written with `createElement`:
118
+
这里是一个完整的使用 `createElement` 的示例:
119
119
120
120
<Sandpack>
121
121
@@ -126,16 +126,16 @@ function Greeting({ name }) {
126
126
returncreateElement(
127
127
'h1',
128
128
{ className:'greeting' },
129
-
'Hello ',
129
+
'你好',
130
130
createElement('i', null, name),
131
-
'. Welcome!'
131
+
',欢迎!'
132
132
);
133
133
}
134
134
135
135
exportdefaultfunctionApp() {
136
136
returncreateElement(
137
137
Greeting,
138
-
{ name:'Taylor' }
138
+
{ name:'泰勒' }
139
139
);
140
140
}
141
141
```
@@ -149,21 +149,21 @@ export default function App() {
149
149
150
150
</Sandpack>
151
151
152
-
And here is the same example written using JSX:
152
+
这里是相同的示例,但使用的是 JSX:
153
153
154
154
<Sandpack>
155
155
156
156
```js
157
157
functionGreeting({ name }) {
158
158
return (
159
159
<h1 className="greeting">
160
-
Hello <i>{name}</i>. Welcome!
160
+
你好<i>{name}</i>,欢迎!
161
161
</h1>
162
162
);
163
163
}
164
164
165
165
exportdefaultfunctionApp() {
166
-
return<Greeting name="Taylor"/>;
166
+
return<Greeting name="泰勒"/>;
167
167
}
168
168
```
169
169
@@ -176,30 +176,30 @@ export default function App() {
176
176
177
177
</Sandpack>
178
178
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.
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:
**Note that creating this object does not render the `Greeting`component or create any DOM elements.**
199
+
**注意,创建这个对象并不会渲染 `Greeting`组件或者创建任何 DOM 元素**。
200
200
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.
0 commit comments