Skip to content

Commit 12579f9

Browse files
merging all conflicts
2 parents 417cc90 + 27d86ff commit 12579f9

File tree

2 files changed

+138
-6
lines changed

2 files changed

+138
-6
lines changed

src/content/reference/react-dom/components/index.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,137 @@ React 支持所有浏览器内置的组件,包括:
162162

163163
### 自定义 HTML 元素 {/*custom-html-elements*/}
164164

165+
<<<<<<< HEAD
165166
如果你渲染一个带有连字符的标签,如 `<my-element>`,React 会认为你想要渲染一个 [自定义 HTML 元素](https://developer.mozilla.org/zh-CN/docs/Web/Web_Components/Using_custom_elements)。在 React 中,渲染自定义元素与渲染内置的浏览器标签有所不同:
166167

167168
- 所有自定义元素的 props 都将被序列化为字符串,并且总是使用属性(attribute)进行设置。
168169
- 自定义元素接受 `class` 而不是 `className`,接受 `for` 而不是 `htmlFor`
170+
=======
171+
If you render a tag with a dash, like `<my-element>`, React will assume you want to render a [custom HTML element.](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)
172+
>>>>>>> 27d86ffe6ec82e3642c6490d2187bae2271020a4
169173
170174
如果你使用 [`is`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/is) 属性渲染一个内置的浏览器 HTML 元素,它也会被视为自定义元素。
171175

176+
#### Setting values on custom elements {/*attributes-vs-properties*/}
177+
178+
Custom elements have two methods of passing data into them:
179+
180+
1) Attributes: Which are displayed in markup and can only be set to string values
181+
2) Properties: Which are not displayed in markup and can be set to arbitrary JavaScript values
182+
183+
By default, React will pass values bound in JSX as attributes:
184+
185+
```jsx
186+
<my-element value="Hello, world!"></my-element>
187+
```
188+
189+
Non-string JavaScript values passed to custom elements will be serialized by default:
190+
191+
```jsx
192+
// Will be passed as `"1,2,3"` as the output of `[1,2,3].toString()`
193+
<my-element value={[1,2,3]}></my-element>
194+
```
195+
196+
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:
197+
198+
<Sandpack>
199+
200+
```js src/index.js hidden
201+
import {MyElement} from './MyElement.js';
202+
import { createRoot } from 'react-dom/client';
203+
import {App} from "./App.js";
204+
205+
customElements.define('my-element', MyElement);
206+
207+
const root = createRoot(document.getElementById('root'))
208+
root.render(<App />);
209+
```
210+
211+
```js src/MyElement.js active
212+
export class MyElement extends HTMLElement {
213+
constructor() {
214+
super();
215+
// The value here will be overwritten by React
216+
// when initialized as an element
217+
this.value = undefined;
218+
}
219+
220+
connectedCallback() {
221+
this.innerHTML = this.value.join(", ");
222+
}
223+
}
224+
```
225+
226+
```js src/App.js
227+
export function App() {
228+
return <my-element value={[1,2,3]}></my-element>
229+
}
230+
```
231+
232+
</Sandpack>
233+
234+
#### Listening for events on custom elements {/*custom-element-events*/}
235+
236+
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.
237+
238+
<Sandpack>
239+
240+
```js src/index.js hidden
241+
import {MyElement} from './MyElement.js';
242+
import { createRoot } from 'react-dom/client';
243+
import {App} from "./App.js";
244+
245+
customElements.define('my-element', MyElement);
246+
247+
const root = createRoot(document.getElementById('root'))
248+
root.render(<App />);
249+
```
250+
251+
```javascript src/MyElement.js
252+
export class MyElement extends HTMLElement {
253+
constructor() {
254+
super();
255+
this.test = undefined;
256+
this.emitEvent = this._emitEvent.bind(this);
257+
}
258+
259+
_emitEvent() {
260+
const event = new CustomEvent('speak', {
261+
detail: {
262+
message: 'Hello, world!',
263+
},
264+
});
265+
this.dispatchEvent(event);
266+
}
267+
268+
connectedCallback() {
269+
this.el = document.createElement('button');
270+
this.el.innerText = 'Say hi';
271+
this.el.addEventListener('click', this.emitEvent);
272+
this.appendChild(this.el);
273+
}
274+
275+
disconnectedCallback() {
276+
this.el.removeEventListener('click', this.emitEvent);
277+
}
278+
}
279+
```
280+
281+
```jsx src/App.js active
282+
export function App() {
283+
return (
284+
<my-element
285+
onspeak={e => console.log(e.detail.message)}
286+
></my-element>
287+
)
288+
}
289+
```
290+
291+
</Sandpack>
292+
172293
<Note>
173294

295+
<<<<<<< HEAD
174296
[未来的 React 版本将提供更全面的自定义元素支持](https://github.com/facebook/react/issues/11347#issuecomment-1122275286)
175297

176298
你可以通过将 React 包升级到最新的实验性版本来尝试:
@@ -179,6 +301,16 @@ React 支持所有浏览器内置的组件,包括:
179301
- `react-dom@experimental`
180302

181303
实验性版本的 React 可能包含一些错误,因此不要在生产环境中使用。
304+
=======
305+
Events are case-sensitive and support dashes (`-`). Preserve the casing of the event and include all dashes when listening for custom element's events:
306+
307+
```jsx
308+
// Listens for `say-hi` events
309+
<my-element onsay-hi={console.log}></my-element>
310+
// Listens for `sayHi` events
311+
<my-element onsayHi={console.log}></my-element>
312+
```
313+
>>>>>>> 27d86ffe6ec82e3642c6490d2187bae2271020a4
182314
183315
</Note>
184316
---

src/content/reference/react/Activity.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ While hidden, children still re-render in response to new props, albeit at a low
5151

5252
When the boundary becomes <CodeStep step={3}>visible</CodeStep> again, React will reveal the children with their previous state restored, and re-create their Effects.
5353

54-
In this way, Activity can thought of as a mechanism for rendering "background activity". Rather than completely discarding content that's likely to become visible again, you can use Activity to maintain and restore that content's UI and internal state, while ensuring hidden content has no unwanted side effects.
54+
In this way, Activity can be thought of as a mechanism for rendering "background activity". Rather than completely discarding content that's likely to become visible again, you can use Activity to maintain and restore that content's UI and internal state, while ensuring that your hidden content has no unwanted side effects.
5555

5656
[See more examples below.](#usage)
5757

@@ -62,15 +62,15 @@ In this way, Activity can thought of as a mechanism for rendering "background ac
6262

6363
#### Caveats {/*caveats*/}
6464

65-
- When used with `<ViewTransition>`, hidden activities that reveal in a transition will activate an "enter" animation. Visible Activities hidden in a transition will activate an "exit" animation.
65+
- If an Activity is rendered inside of a [ViewTransition](/reference/react/ViewTransition), and it becomes visible as a result of an update caused by [startTransition](/reference/react/startTransition), it will activate the ViewTransition's `enter` animation. If it becomes hidden, it will activate its `exit` animation.
6666

6767
---
6868

6969
## Usage {/*usage*/}
7070

7171
### Restoring the state of hidden components {/*restoring-the-state-of-hidden-components*/}
7272

73-
Typically in React, when you want to conditionally show or hide a component, you mount and unmount it:
73+
In React, when you want to conditionally show or hide a component, you typically mount or unmount it based on that condition:
7474

7575
```jsx
7676
{isShowingSidebar && (
@@ -88,11 +88,11 @@ When you hide a component using an Activity boundary instead, React will "save"
8888
</Activity>
8989
```
9090

91-
This makes it possible to restore components to their previous state.
91+
This makes it possible to hide and then later restore components in the state they were previously in.
9292

93-
The following example has a sidebar with an expandable section – you can press "Overview" to reveal the three subitems below it. The main app area also has a button that hides and shows the sidebar.
93+
The following example has a sidebar with an expandable section. You can press "Overview" to reveal the three subitems below it. The main app area also has a button that hides and shows the sidebar.
9494

95-
Try expanding the Overview section, then toggling the sidebar closed and open:
95+
Try expanding the Overview section, and then toggling the sidebar closed then open:
9696

9797
<Sandpack>
9898

0 commit comments

Comments
 (0)