Skip to content

Commit e56579a

Browse files
YucohnyHuxproXleine
authored
docs(cn): translate reference/react-dom/findDOMNode into Chinese (#1203)
Co-authored-by: Xuan Huang (黄玄) <huxpro@gmail.com> Co-authored-by: Xleine <919143384@qq.com>
1 parent c536e9f commit e56579a

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

src/content/reference/react-dom/findDOMNode.md

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: findDOMNode
44

55
<Deprecated>
66

7-
This API will be removed in a future major version of React. [See the alternatives.](#alternatives)
7+
API 将在未来的 React 主要版本中被移除。[请查看替代方案](#alternatives)
88

99
</Deprecated>
1010

1111
<Intro>
1212

13-
`findDOMNode` finds the browser DOM node for a React [class component](/reference/react/Component) instance.
13+
`findDOMNode` 方法可以获取 [类式组件](/reference/react/Component) 实例对应的浏览器 DOM 节点。
1414

1515
```js
1616
const domNode = findDOMNode(componentInstance)
@@ -22,46 +22,46 @@ const domNode = findDOMNode(componentInstance)
2222

2323
---
2424

25-
## Reference {/*reference*/}
25+
## 参考 {/*reference*/}
2626

2727
### `findDOMNode(componentInstance)` {/*finddomnode*/}
2828

29-
Call `findDOMNode` to find the browser DOM node for a given React [class component](/reference/react/Component) instance.
29+
使用 `findDOMNode` 获取 [类式组件](/reference/react/Component) 实例对应的浏览器 DOM 节点。
3030

3131
```js
3232
import { findDOMNode } from 'react-dom';
3333

3434
const domNode = findDOMNode(componentInstance);
3535
```
3636

37-
[See more examples below.](#usage)
37+
[参见下面更多示例](#usage)
3838

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

41-
* `componentInstance`: An instance of the [`Component`](/reference/react/Component) subclass. For example, `this` inside a class component.
41+
* `componentInstance`[`Component`](/reference/react/Component) 子类的实例。举个例子,类式组件中的 `this`
4242

4343

44-
#### Returns {/*returns*/}
44+
#### 返回值 {/*returns*/}
4545

46-
`findDOMNode` returns the first closest browser DOM node within the given `componentInstance`. When a component renders to `null`, or renders `false`, `findDOMNode` returns `null`. When a component renders to a string, `findDOMNode` returns a text DOM node containing that value.
46+
`findDOMNode` 方法返回与给定的 `componentInstance` 中最接近的浏览器 DOM 节点。当组件渲染为 `null``false` 时,`findDOMNode` 返回 `null`。当组件渲染为字符串时,`findDOMNode` 返回一个包含该值的文本 DOM 节点。
4747

48-
#### Caveats {/*caveats*/}
48+
#### 注意 {/*caveats*/}
4949

50-
* A component may return an array or a [Fragment](/reference/react/Fragment) with multiple children. In that case `findDOMNode`, will return the DOM node corresponding to the first non-empty child.
50+
* 组件可能会返回包含多个子元素的数组或 [Fragment](/reference/react/Fragment)。在这种情况下,`findDOMNode` 会返回第一个非空子节点对应的 DOM 节点。
5151

52-
* `findDOMNode` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `findDOMNode()` in `render()` on a component that has yet to be created), an exception will be thrown.
52+
* `findDOMNode` 只对已经挂载到 DOM 上的组件有效。如果你尝试在一个还未挂载的组件上调用 `findDOMNode()`(比如在一个还未创建的组件的 `render()` 方法中调用 `findDOMNode()`),会抛出异常。
5353

54-
* `findDOMNode` only returns the result at the time of your call. If a child component renders a different node later, there is no way for you to be notified of this change.
54+
* `findDOMNode` 只会返回调用时的结果,你无法得知组件是否在之后渲染了不同的节点。
5555

56-
* `findDOMNode` accepts a class component instance, so it can't be used with function components.
56+
* `findDOMNode` 接受类组件实例作为参数,而不能用于函数式组件。
5757

5858
---
5959

60-
## Usage {/*usage*/}
60+
## 用法 {/*usage*/}
6161

62-
### Finding the root DOM node of a class component {/*finding-the-root-dom-node-of-a-class-component*/}
62+
### 寻找类式组件对应的 DOM 节点 {/*finding-the-root-dom-node-of-a-class-component*/}
6363

64-
Call `findDOMNode` with a [class component](/reference/react/Component) instance (usually, `this`) to find the DOM node it has rendered.
64+
使用 `findDOMNode` 获取 [类式组件](/reference/react/Component) 实例(通常是 `this`)对应的已渲染 DOM 节点。
6565

6666
```js {3}
6767
class AutoselectingInput extends Component {
@@ -71,12 +71,12 @@ class AutoselectingInput extends Component {
7171
}
7272

7373
render() {
74-
return <input defaultValue="Hello" />
74+
return <input defaultValue="你好" />
7575
}
7676
}
7777
```
7878

79-
Here, the `input` variable will be set to the `<input>` DOM element. This lets you do something with it. For example, when clicking "Show example" below mounts the input, [`input.select()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select) selects all text in the input:
79+
在这里,`input` 变量将被设置为 `<input>` DOM 元素,这样你就可以对其进行操作。例如,当点击下方的“显示示例”按钮并挂载了输入框后,[`input.select()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select) 会选中输入框中的所有文本:
8080

8181
<Sandpack>
8282

@@ -89,7 +89,7 @@ export default function App() {
8989
return (
9090
<>
9191
<button onClick={() => setShow(true)}>
92-
Show example
92+
显示示例
9393
</button>
9494
<hr />
9595
{show && <AutoselectingInput />}
@@ -109,7 +109,7 @@ class AutoselectingInput extends Component {
109109
}
110110

111111
render() {
112-
return <input defaultValue="Hello" />
112+
return <input defaultValue="你好" />
113113
}
114114
}
115115

@@ -120,11 +120,11 @@ export default AutoselectingInput;
120120

121121
---
122122

123-
## Alternatives {/*alternatives*/}
123+
## 替代方案 {/*alternatives*/}
124124

125-
### Reading component's own DOM node from a ref {/*reading-components-own-dom-node-from-a-ref*/}
125+
### 使用 ref 读取组件自己的 DOM 节点 {/*reading-components-own-dom-node-from-a-ref*/}
126126

127-
Code using `findDOMNode` is fragile because the connection between the JSX node and the code manipulating the corresponding DOM node is not explicit. For example, try wrapping this `<input />` into a `<div>`:
127+
由于 JSX 节点与操作相应的 DOM 节点的代码之间的联系不是显式的,因此使用 `findDOMNode` 的代码非常脆弱。例如,尝试将此 `<input />` 包装在一个 `<div>` 中:
128128

129129
<Sandpack>
130130

@@ -137,7 +137,7 @@ export default function App() {
137137
return (
138138
<>
139139
<button onClick={() => setShow(true)}>
140-
Show example
140+
显示示例
141141
</button>
142142
<hr />
143143
{show && <AutoselectingInput />}
@@ -165,9 +165,9 @@ export default AutoselectingInput;
165165

166166
</Sandpack>
167167

168-
This will break the code because now, `findDOMNode(this)` finds the `<div>` DOM node, but the code expects an `<input>` DOM node. To avoid these kinds of problems, use [`createRef`](/reference/react/createRef) to manage a specific DOM node.
168+
这将出现问题。`findDOMNode(this)` 找到的是 `<div>` 节点,但其实我们期望找到的是 `<input>` 节点。为了避免这些问题,考虑使用 [`createRef`](/reference/react/createRef) 管理特定的 DOM 节点。
169169

170-
In this example, `findDOMNode` is no longer used. Instead, `inputRef = createRef(null)` is defined as an instance field on the class. To read the DOM node from it, you can use `this.inputRef.current`. To attach it to the JSX, you render `<input ref={this.inputRef} />`. This connects the code using the DOM node to its JSX:
170+
在这个例子中不再使用 `findDOMNode`。相反,使用 `inputRef = createRef(null)` 并将其定义为类的实例字段。如果想要从中读取 DOM 节点,可以使用`this.inputRef.current`。如果想要将其附加在 JSX 上,考虑渲染 `<input ref={this.inputRef} />`。这将连接使用 DOM 节点的代码与对应 JSX
171171

172172
<Sandpack>
173173

@@ -180,7 +180,7 @@ export default function App() {
180180
return (
181181
<>
182182
<button onClick={() => setShow(true)}>
183-
Show example
183+
显示示例
184184
</button>
185185
<hr />
186186
{show && <AutoselectingInput />}
@@ -202,7 +202,7 @@ class AutoselectingInput extends Component {
202202

203203
render() {
204204
return (
205-
<input ref={this.inputRef} defaultValue="Hello" />
205+
<input ref={this.inputRef} defaultValue="你好" />
206206
);
207207
}
208208
}
@@ -212,7 +212,7 @@ export default AutoselectingInput;
212212

213213
</Sandpack>
214214

215-
In modern React without class components, the equivalent code would call [`useRef`](/reference/react/useRef) instead:
215+
在函数式组件中,你应该使用 [`useRef`](/reference/react/useRef)
216216

217217
<Sandpack>
218218

@@ -225,7 +225,7 @@ export default function App() {
225225
return (
226226
<>
227227
<button onClick={() => setShow(true)}>
228-
Show example
228+
显示示例
229229
</button>
230230
<hr />
231231
{show && <AutoselectingInput />}
@@ -245,19 +245,19 @@ export default function AutoselectingInput() {
245245
input.select();
246246
}, []);
247247

248-
return <input ref={inputRef} defaultValue="Hello" />
248+
return <input ref={inputRef} defaultValue="你好" />
249249
}
250250
```
251251

252252
</Sandpack>
253253

254-
[Read more about manipulating the DOM with refs.](/learn/manipulating-the-dom-with-refs)
254+
如果你想了解更多,请阅读 [使用 ref 操作 DOM](/learn/manipulating-the-dom-with-refs)
255255

256256
---
257257

258-
### Reading a child component's DOM node from a forwarded ref {/*reading-a-child-components-dom-node-from-a-forwarded-ref*/}
258+
### 使用 ref 操作子组件的 DOM 节点 {/*reading-a-child-components-dom-node-from-a-forwarded-ref*/}
259259

260-
In this example, `findDOMNode(this)` finds a DOM node that belongs to another component. The `AutoselectingInput` renders `MyInput`, which is your own component that renders a browser `<input>`.
260+
在这个例子中,`findDOMNode(this)` 获取了属于另一个组件的 DOM 节点。`AutoselectingInput` 渲染了我们自己的 `MyInput` 组件,而 `MyInput` 渲染了浏览器标签 `<input>`
261261

262262
<Sandpack>
263263

@@ -270,7 +270,7 @@ export default function App() {
270270
return (
271271
<>
272272
<button onClick={() => setShow(true)}>
273-
Show example
273+
显示示例
274274
</button>
275275
<hr />
276276
{show && <AutoselectingInput />}
@@ -299,20 +299,20 @@ export default AutoselectingInput;
299299

300300
```js MyInput.js
301301
export default function MyInput() {
302-
return <input defaultValue="Hello" />;
302+
return <input defaultValue="你好" />;
303303
}
304304
```
305305

306306
</Sandpack>
307307

308-
Notice that calling `findDOMNode(this)` inside `AutoselectingInput` still gives you the DOM `<input>`--even though the JSX for this `<input>` is hidden inside the `MyInput` component. This seems convenient for the above example, but it leads to fragile code. Imagine that you wanted to edit `MyInput` later and add a wrapper `<div>` around it. This would break the code of `AutoselectingInput` (which expects to find an `<input>`).
308+
请注意,即使 `<input>` 被隐藏在 `MyInput` 组件中,在 `AutoselectingInput` 中调用 `findDOMNode(this)` 仍然会返回 `<input>`。这在上面的例子中似乎很方便,但它会导致代码变得非常脆弱。想象一下,如果你以后想编辑 `MyInput` 并使用 `<div>` 包装,但是这将会破坏 `AutoselectingInput` 的代码(因为它期望找到 `<input>`)。
309309

310-
To replace `findDOMNode` in this example, the two components need to coordinate:
310+
考虑在这个例子中替换 `findDOMNode`,并且下列两个组件需要协同工作:
311311

312-
1. `AutoSelectingInput` should declare a ref, like [in the earlier example](#reading-components-own-dom-node-from-a-ref), and pass it to `<MyInput>`.
313-
2. `MyInput` should be declared with [`forwardRef`](/reference/react/forwardRef) to take that ref and forward it down to the `<input>` node.
312+
1. `AutoSelectingInput` 中声明一个 ref,就像 [前面的例子](#reading-components-own-dom-node-from-a-ref) 中一样,并将其传递给 `<MyInput>`
313+
2. `MyInput` 使用 [`forwardRef`](/reference/react/forwardRef) 接收该 ref 并将其转发到 `<input>` 节点。
314314

315-
This version does that, so it no longer needs `findDOMNode`:
315+
这种方式解决了这个问题,所以不再需要 `findDOMNode`
316316

317317
<Sandpack>
318318

@@ -325,7 +325,7 @@ export default function App() {
325325
return (
326326
<>
327327
<button onClick={() => setShow(true)}>
328-
Show example
328+
显示示例
329329
</button>
330330
<hr />
331331
{show && <AutoselectingInput />}
@@ -360,15 +360,15 @@ export default AutoselectingInput;
360360
import { forwardRef } from 'react';
361361

362362
const MyInput = forwardRef(function MyInput(props, ref) {
363-
return <input ref={ref} defaultValue="Hello" />;
363+
return <input ref={ref} defaultValue="你好" />;
364364
});
365365

366366
export default MyInput;
367367
```
368368

369369
</Sandpack>
370370

371-
Here is how this code would look like with function components instead of classes:
371+
以下是上面代码在函数式组件中的样子:
372372

373373
<Sandpack>
374374

@@ -381,7 +381,7 @@ export default function App() {
381381
return (
382382
<>
383383
<button onClick={() => setShow(true)}>
384-
Show example
384+
显示示例
385385
</button>
386386
<hr />
387387
{show && <AutoselectingInput />}
@@ -402,15 +402,15 @@ export default function AutoselectingInput() {
402402
input.select();
403403
}, []);
404404

405-
return <MyInput ref={inputRef} defaultValue="Hello" />
405+
return <MyInput ref={inputRef} defaultValue="你好" />
406406
}
407407
```
408408

409409
```js MyInput.js
410410
import { forwardRef } from 'react';
411411

412412
const MyInput = forwardRef(function MyInput(props, ref) {
413-
return <input ref={ref} defaultValue="Hello" />;
413+
return <input ref={ref} defaultValue="你好" />;
414414
});
415415

416416
export default MyInput;
@@ -420,16 +420,16 @@ export default MyInput;
420420

421421
---
422422

423-
### Adding a wrapper `<div>` element {/*adding-a-wrapper-div-element*/}
423+
### 使用 `<div>` 包装 {/*adding-a-wrapper-div-element*/}
424424

425-
Sometimes a component needs to know the position and size of its children. This makes it tempting to find the children with `findDOMNode(this)`, and then use DOM methods like [`getBoundingClientRect`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) for measurements.
425+
有时,一个组件想要知道子元素的位置和大小。这会让你想要使用 `findDOMNode(this)` 查找子元素,然后使用 [`getBoundingClientRect`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) 等 DOM 方法来进行测量。
426426

427-
There is currently no direct equivalent for this use case, which is why `findDOMNode` is deprecated but is not yet removed completely from React. In the meantime, you can try rendering a wrapper `<div>` node around the content as a workaround, and getting a ref to that node. However, extra wrappers can break styling.
427+
目前,还没有直接适用于此场景的替代方法,这就是为什么 `findDOMNode` 已弃用但尚未从 React 中完全删除的原因。在此期间,你可以尝试在内容周围使用 `<div>` 包装,并向其添加 ref。但是,额外的包装可能会破坏样式。
428428

429429
```js
430430
<div ref={someRef}>
431431
{children}
432432
</div>
433433
```
434434

435-
This also applies to focusing and scrolling to arbitrary children.
435+
这对于聚焦以及滚动到任何子元素也是一样。

0 commit comments

Comments
 (0)