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
They are special in React because React can render them into the document head, suspend while resources are loading, and enact other behaviors that are described on the reference page for each specific component.
如果你使用 [`is`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/is) 属性渲染一个内置的浏览器 HTML 元素,它也会被视为自定义元素。
168
168
169
-
#### Setting values on custom elements {/*attributes-vs-properties*/}
169
+
#### 在自定义元素上设置值 {/*attributes-vs-properties*/}
170
170
171
-
Custom elements have two methods of passing data into them:
171
+
自定义元素有两种数据传递方法:
172
172
173
-
1) Attributes: Which are displayed in markup and can only be set to string values
174
-
2) Properties: Which are not displayed in markup and can be set to arbitrary JavaScript values
173
+
1) Attributes:显示在标记中,只能设置为字符串值
174
+
2) Properties:不在标记中显示,可设置为任意 JavaScript 值
175
175
176
-
By default, React will pass values bound in JSX as attributes:
176
+
默认情况下,React 会将 JSX 中绑定的值作为 attributes 传递:
177
177
178
178
```jsx
179
179
<my-element value="Hello, world!"></my-element>
180
180
```
181
181
182
-
Non-string JavaScript values passed to custom elements will be serialized by default:
182
+
默认情况下,传递给自定义元素的非字符串 JavaScript 值将被序列化:
183
183
184
184
```jsx
185
-
//Will be passed as `"1,2,3"` as the output of `[1,2,3].toString()`
185
+
//将以 `[1,2,3].toString()` 的输出结果 `"1,2,3"` 的形式传递
186
186
<my-element value={[1,2,3]}></my-element>
187
187
```
188
188
189
-
React will, however, recognize an custom element's property as one that it may pass arbitrary values to if the property name shows up on the class during construction:
#### Listening for events on custom elements {/*custom-element-events*/}
227
+
#### 监听自定义元素上的事件 {/*custom-element-events*/}
228
228
229
-
A common pattern when using custom elements is that they may dispatch [`CustomEvent`s](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) rather than accept a function to call when an event occur. You can listen for these events using an `on`prefix when binding to the event via JSX.
Events are case-sensitive and support dashes (`-`). Preserve the casing of the event and include all dashes when listening for custom element's events:
0 commit comments