From 34ae36c90701b94049e6da54df0b6518da1039a7 Mon Sep 17 00:00:00 2001 From: Aitor Lancharro Date: Fri, 14 Oct 2022 19:22:10 +0200 Subject: [PATCH 1/2] forwardRef --- beta/src/content/apis/react/forwardRef.md | 88 +++++++++++------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/beta/src/content/apis/react/forwardRef.md b/beta/src/content/apis/react/forwardRef.md index 6a6287b68..e527f7c30 100644 --- a/beta/src/content/apis/react/forwardRef.md +++ b/beta/src/content/apis/react/forwardRef.md @@ -4,7 +4,7 @@ title: forwardRef -`forwardRef` lets your component expose a DOM node to parent component with a [ref.](/learn/manipulating-the-dom-with-refs) +`forwardRef` permite a su componente exponer un nodo DOM al componente padre con un [ref.](/learn/manipulating-the-dom-with-refs) ```js const SomeComponent = forwardRef(render) @@ -16,11 +16,11 @@ const SomeComponent = forwardRef(render) --- -## Usage {/*usage*/} +## Uso {/*usage*/} -### Exposing a DOM node to the parent component {/*exposing-a-dom-node-to-the-parent-component*/} +### Exponer un nodo DOM al componente padre {/*exposing-a-dom-node-to-the-parent-component*/} -By default, each component's DOM nodes are private. However, sometimes it's useful to expose a DOM node to the parent--for example, to allow focusing it. To opt in, wrap your component definition into `forwardRef()`: +Por defecto, los nodos DOM de cada componente son privados. Sin embargo, a veces es útil exponer un nodo DOM al padre, por ejemplo, para permitir su enfoque. Para optar por ello, envuelve la definición de su componente en `forwardRef()`: ```js {3,11} import { forwardRef } from 'react'; @@ -36,7 +36,7 @@ const MyInput = forwardRef(function MyInput(props, ref) { }); ``` -You will receive a ref as the second argument after props. Pass it to the DOM node that you want to expose: +Recibirás un ref como segundo argumento después de props. Pásalo al nodo DOM que quieras exponer: ```js {8} [[1, 3, "ref"], [1, 8, "ref", 30]] import { forwardRef } from 'react'; @@ -52,7 +52,7 @@ const MyInput = forwardRef(function MyInput(props, ref) { }); ``` -This lets the parent `Form` component access the `` DOM node exposed by `MyInput`: +Esto permite al componente padre `Form` acceder al `` nodo DOM expuesto por `MyInput`: ```js [[1, 2, "ref"], [1, 10, "ref", 41], [2, 5, "ref.current"]] function Form() { @@ -73,15 +73,15 @@ function Form() { } ``` -This `Form` component [passes a ref](/apis/useref#manipulating-the-dom-with-a-ref) to `MyInput`. The `MyInput` component *forwards* that ref to the `` browser tag. As a result, the `Form` component can access that `` DOM node and call [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on it. +Éste componente `Form` [pasa un ref](/apis/useref#manipulating-the-dom-with-a-ref) a `MyInput`. El componente `MyInput` *envía* esa referencia al tag `` del navegador. Como resultado, el componente `Form` puede acceder a ese nodo DOM `` y llamar a [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) en él. -Keep in mind that by exposing a ref to the DOM node inside your component, you're making it harder to change your component's internals later. You will typically expose DOM nodes from reusable low-level components like buttons or text inputs, but you won't do it for application-level components like an avatar or a comment. +Ten en cuenta que al exponer una referencia al nodo DOM dentro de tu componente, estás dificultando la posibilidad de cambiar el interior de tu componente más adelante. Por lo general, expondrás los nodos DOM de los componentes reutilizables de bajo nivel como los botones o las entradas de texto, pero no lo harás para los componentes de nivel de aplicación como un avatar o un comentario. -#### Focusing a text input {/*focusing-a-text-input*/} +#### Enfocar una entrada de texto {/*focusing-a-text-input*/} -Clicking the button will focus the input. The `Form` component defines a ref and passes it to the `MyInput` component. The `MyInput` component fowards that ref to the browser ``. This lets the `Form` component focus the ``. +Al hacer clic en el botón se centrará la entrada. El componente `Form` define una referencia y la pasa al componente `MyInput`. El componente `MyInput` reenvía esa referencia al navegador ``. Esto permite que el componente `Form` enfoque el ``. @@ -133,9 +133,9 @@ input { -#### Playing and pausing a video {/*playing-and-pausing-a-video*/} +#### Reproducir y pausar un vídeo {/*playing-and-pausing-a-video*/} -Clicking the button will call [`play()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play) and [`pause()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/pause) on a `