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
Copy file name to clipboardExpand all lines: content/docs/portals.md
+34-34Lines changed: 34 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,21 @@ title: Portals
4
4
permalink: docs/portals.html
5
5
---
6
6
7
-
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
7
+
Portal 提供了一种将子节点渲染到存在于父组件以外的 DOM 节点的优秀的方案。
8
8
9
9
```js
10
10
ReactDOM.createPortal(child, container)
11
11
```
12
12
13
-
The first argument (`child`) is any [renderable React child](/docs/react-component.html#render), such as an element, string, or fragment. The second argument (`container`) is a DOM element.
13
+
第一个参数(`child`)是任何[可渲染的 React 子元素](/docs/react-component.html#render),例如一个元素,字符串或 fragment。第二个参数(`container`)是一个 DOM 元素。
14
14
15
-
## Usage {#usage}
15
+
## 用法 {#usage}
16
16
17
-
Normally, when you return an element from a component's render method, it's mounted into the DOM as a child of the nearest parent node:
17
+
通常来讲,当你从组件的 render 方法返回一个元素时,该元素将被挂载到 DOM 节点中离其最近的父节点:
18
18
19
19
```js{4,6}
20
20
render() {
21
-
// React mounts a new div and renders the children into it
21
+
// React 挂载了一个新的 div,并且把子元素渲染其中
22
22
return (
23
23
<div>
24
24
{this.props.children}
@@ -27,34 +27,34 @@ render() {
27
27
}
28
28
```
29
29
30
-
However, sometimes it's useful to insert a child into a different location in the DOM:
30
+
然而,有时候将子元素插入到 DOM 节点中的不同位置也是有好处的:
31
31
32
32
```js{6}
33
33
render() {
34
-
// React does *not* create a new div. It renders the children into `domNode`.
35
-
// `domNode` is any valid DOM node, regardless of its location in the DOM.
34
+
// React 并*没有*创建一个新的 div。它只是把子元素渲染到 `domNode` 中。
35
+
// `domNode` 是一个可以在任何位置的有效 DOM 节点。
36
36
return ReactDOM.createPortal(
37
37
this.props.children,
38
38
domNode
39
39
);
40
40
}
41
41
```
42
42
43
-
A typical use case for portals is when a parent component has an `overflow: hidden`or`z-index`style, but you need the child to visually "break out" of its container. For example, dialogs, hovercards, and tooltips.
> When working with portals, remember that [managing keyboard focus](/docs/accessibility.html#programmatically-managing-focus) becomes very important.
48
-
>
49
-
> For modal dialogs, ensure that everyone can interact with them by following the [WAI-ARIA Modal Authoring Practices](https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal).
Even though a portal can be anywhere in the DOM tree, it behaves like a normal React child in every other way. Features like context work exactly the same regardless of whether the child is a portal, as the portal still exists in the *React tree* regardless of position in the *DOM tree*.
This includes event bubbling. An event fired from inside a portal will propagate to ancestors in the containing *React tree*, even if those elements are not ancestors in the *DOM tree*. Assuming the following HTML structure:
57
+
这包含事件冒泡。一个从 portal 内部触发的事件会一直冒泡至包含 *React 树*的祖先,即便这些元素并不是 *DOM 树* 中的祖先。假设存在如下 HTML 结构:
58
58
59
59
```html
60
60
<html>
@@ -65,10 +65,10 @@ This includes event bubbling. An event fired from inside a portal will propagate
65
65
</html>
66
66
```
67
67
68
-
A `Parent` component in `#app-root`would be able to catch an uncaught, bubbling event from the sibling node `#modal-root`.
Catching an event bubbling up from a portal in a parent component allows the development of more flexible abstractions that are not inherently reliant on portals. For example, if you render a `<Modal />`component, the parent can capture its events regardless of whether it's implemented using portals.
0 commit comments