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
* Adding translations to the findDOMNode API
* Update beta/src/content/reference/react-dom/findDOMNode.md
Keeping the links to other sections with their original text.
Co-authored-by: Rainer Martinez <[email protected]>
* Apply suggestions from code review
---------
Co-authored-by: Rainer Martinez <[email protected]>
Call `findDOMNode`to find the browser DOM node for a given React [class component](/reference/react/Component)instance.
29
+
Llama a `findDOMNode`para encontrar el nodo DOM del navegador para una instancia dada de un [componente de clase](/reference/react/Component)de React.
30
30
31
31
```js
32
32
import { findDOMNode } from'react-dom';
33
33
34
34
constdomNode=findDOMNode(componentInstance);
35
35
```
36
36
37
-
[See more examples below.](#usage)
37
+
[Consulta más ejemplos debajo.](#usage)
38
38
39
-
#### Parameters {/*parameters*/}
39
+
#### Parámetros {/*parameters*/}
40
40
41
-
*`componentInstance`: An instance of the [`Component`](/reference/react/Component) subclass. For example, `this`inside a class component.
41
+
*`componentInstance`: Una instancia de la subclase [`Componente`](/reference/react/Component). Por ejemplo, `this`dentro de un componente de clase.
42
42
43
43
44
44
#### Returns {/*returns*/}
45
45
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`devuelve el primer nodo DOM del navegador más cercano dentro del `componentInstance` dado. Cuando un componente renderiza `null` o `false`, `findDOMNode`devuelve`null`. Cuando un componente renderiza un string, `findDOMNode`devuelve un nodo DOM de texto que contiene ese valor.
47
47
48
-
#### Caveats {/*caveats*/}
48
+
#### Advertencias {/*caveats*/}
49
49
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
+
*Un componente puede devolver un array o un[Fragment](/reference/react/Fragment)con múltiples hijos. En este caso,`findDOMNode` devolverá el nodo DOM correspondiente al primer hijo no vacío.
51
51
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`solo funciona en componentes montados (es decir, componentes que se han colocado en el DOM). Si intentas llamar a esto en un componente que aún no se ha montado (como llamar a `findDOMNode()`en`render()`en un componente que aún no se ha creado), se lanzará una excepción.
53
53
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`solo devuelve el resultado en el momento de tu llamada. Si un componente hijo representa un nodo diferente más tarde, no hay manera de que se te notifique de este cambio.
55
55
56
-
*`findDOMNode`accepts a class component instance, so it can't be used with function components.
56
+
*`findDOMNode`acepta una instancia de componente de clase, por lo que no se puede usar con componentes de función.
57
57
58
58
---
59
59
60
-
## Usage {/*usage*/}
60
+
## Uso {/*usage*/}
61
61
62
-
### Finding the root DOM node of a class component {/*finding-the-root-dom-node-of-a-class-component*/}
62
+
### Encontrar el nodo DOM raíz de un componente de clase {/*finding-the-root-dom-node-of-a-class-component*/}
63
63
64
-
Call `findDOMNode`with a [class component](/reference/react/Component)instance (usually, `this`) to find the DOM node it has rendered.
64
+
Llama a `findDOMNode`con una instancia de un [componente de clase](/reference/react/Component)(por lo general, `this`) para encontrar el nodo DOM que ha renderizado.
65
65
66
66
```js {3}
67
67
classAutoselectingInputextendsComponent {
@@ -71,12 +71,12 @@ class AutoselectingInput extends Component {
71
71
}
72
72
73
73
render() {
74
-
return<input defaultValue="Hello"/>
74
+
return<input defaultValue="Hola"/>
75
75
}
76
76
}
77
77
```
78
78
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
+
Aquí, la variable `input`se establecerá en el elemento DOM`<input>`. Esto te permite hacer algo con él. Por ejemplo, al hacer clic en "Mostrar ejemplo" a continuación, se monta el input, [`input.select()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select)selecciona todo el texto del input:
80
80
81
81
<Sandpack>
82
82
@@ -89,7 +89,7 @@ export default function App() {
89
89
return (
90
90
<>
91
91
<button onClick={() =>setShow(true)}>
92
-
Show example
92
+
Mostrar ejemplo
93
93
</button>
94
94
<hr />
95
95
{show &&<AutoselectingInput />}
@@ -109,7 +109,7 @@ class AutoselectingInput extends Component {
### Reading component's own DOM node from a ref {/*reading-components-own-dom-node-from-a-ref*/}
125
+
### Leyendo el nodo DOM propio de un componente a través de una referencia {/*reading-components-own-dom-node-from-a-ref*/}
126
126
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 `<input />`from this example into a`<div>`:
127
+
El código que utiliza `findDOMNode`es frágil debido a que la conexión entre el nodo JSX y el código que manipula el nodo DOM correspondiente no es explícita. Por ejemplo, prueba a envolver `<input />`de este ejemplo en un`<div>`:
128
128
129
129
<Sandpack>
130
130
@@ -137,7 +137,7 @@ export default function App() {
137
137
return (
138
138
<>
139
139
<button onClick={() =>setShow(true)}>
140
-
Show example
140
+
Mostrar ejemplo
141
141
</button>
142
142
<hr />
143
143
{show &&<AutoselectingInput />}
@@ -156,7 +156,7 @@ class AutoselectingInput extends Component {
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
+
Esto romperá el código porque ahora, `findDOMNode(this)`encuentra el nodo DOM `<div>`, pero el código espera un nodo DOM`<input>`. Para evitar este tipo de problemas, utiliza[`createRef`](/reference/react/createRef)para gestionar un nodo DOM específico.
169
169
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} />`. You have connected the code using the DOM node to its JSX:
170
+
En este ejemplo, ya no se usa `findDOMNode`. En su lugar, se define`inputRef = createRef(null)`como un campo de instancia en la clase. Para leer el nodo DOM de él, puedes usar `this.inputRef.current`. Para adjuntarlo al JSX, renderiza `<input ref={this.inputRef} />`. Has conectado el código que usa el nodo DOM con su JSX:
171
171
172
172
<Sandpack>
173
173
@@ -180,7 +180,7 @@ export default function App() {
180
180
return (
181
181
<>
182
182
<button onClick={() =>setShow(true)}>
183
-
Show example
183
+
Mostrar ejemplo
184
184
</button>
185
185
<hr />
186
186
{show &&<AutoselectingInput />}
@@ -202,7 +202,7 @@ class AutoselectingInput extends Component {
[Read more about manipulating the DOM with refs.](/learn/manipulating-the-dom-with-refs)
254
+
[Lee más sobre cómo manipular el DOM con refs.](/learn/manipulating-the-dom-with-refs)
255
255
256
256
---
257
257
258
-
### Reading a child component's DOM node from a forwarded ref {/*reading-a-child-components-dom-node-from-a-forwarded-ref*/}
258
+
### Leer el nodo DOM de un componente hijo desde una ref reenviada {/*reading-a-child-components-dom-node-from-a-forwarded-ref*/}
259
259
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
+
En este ejemplo, `findDOMNode(this)`encuentra un nodo DOM que pertenece a otro componente. El`AutoselectingInput`renderiza`MyInput`, que es tu propio componente que representa una entrada del navegador`<input>`.
261
261
262
262
<Sandpack>
263
263
@@ -270,7 +270,7 @@ export default function App() {
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>` DOM node).
308
+
Ten en cuenta que llamar a `findDOMNode(this)`dentro de `AutoselectingInput`aún te da el nodo DOM `<input>`, aunque el JSX para este`<input>`está oculto dentro del componente `MyInput`. Esto parece conveniente para el ejemplo anterior, pero conduce a un código frágil. Imagínate que deseas editar `MyInput`más tarde y agregar un elemento `<div>`envuelto alrededor de él. Esto rompería el código de `AutoselectingInput` (que espera encontrar un nodo DOM `<input>`).
309
309
310
-
To replace`findDOMNode`in this example, the two components need to coordinate:
310
+
Para reemplazar`findDOMNode`en este ejemplo, los dos componentes deben coordinarse:
311
311
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 read the passed ref, and pass it down to the `<input>` node.
312
+
1.`AutoSelectingInput`debe declarar una ref, como [en el ejemplo anterior](#reading-components-own-dom-node-from-a-ref), y pasarlo a`<MyInput>`.
313
+
2.`MyInput`debe ser declarado con[`forwardRef`](/reference/react/forwardRef)para leer la ref pasado y pasarla hacia abajo al nodo `<input>`.
314
314
315
-
This version does that, so it no longer needs`findDOMNode`:
315
+
Esta versión hace eso, por lo que ya no necesita`findDOMNode`:
316
316
317
317
<Sandpack>
318
318
@@ -325,7 +325,7 @@ export default function App() {
### Adding a wrapper `<div>` element {/*adding-a-wrapper-div-element*/}
423
+
### Agregar un elemento envoltorio `<div>` {/*adding-a-wrapper-div-element*/}
424
424
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
+
A veces, un componente necesita conocer la posición y el tamaño de sus hijos. Esto hace tentador encontrar a los hijos con `findDOMNode(this)`, y luego usar métodos DOM como [`getBoundingClientRect`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect)para las mediciones.
426
426
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 sometimes break styling.
427
+
Actualmente, no hay un equivalente directo para este uso, por lo que`findDOMNode`está en desuso pero aún no se ha eliminado completamente de React. Mientras tanto, puedes intentar renderizar un nodo envoltorio`<div>`alrededor del contenido como una solución temporal y obtener una referencia a ese nodo. Sin embargo, los envoltorios adicionales a veces pueden romper el estilo.
428
428
429
429
```js
430
430
<div ref={someRef}>
431
431
{children}
432
432
</div>
433
433
```
434
434
435
-
This also applies to focusing and scrolling to arbitrary children.
435
+
Esto también se aplica al enfoque y desplazamiento a hijos arbitrarios
0 commit comments