diff --git a/src/content/reference/react/forwardRef.md b/src/content/reference/react/forwardRef.md index 04d69287c..6c8c9390a 100644 --- a/src/content/reference/react/forwardRef.md +++ b/src/content/reference/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` Permitira que seu componente exponha um nó DOM para o componente pai com um [ref.](/learn/manipulating-the-dom-with-refs) ```js const SomeComponent = forwardRef(render) @@ -16,11 +16,11 @@ const SomeComponent = forwardRef(render) --- -## Reference {/*reference*/} +## Referência {/*reference*/} ### `forwardRef(render)` {/*forwardref*/} -Call `forwardRef()` to let your component receive a ref and forward it to a child component: +Invoque `forwardRef()` para criar um componente que pode receber um `ref` e repassá-lo para um componente filho: ```js import { forwardRef } from 'react'; @@ -30,27 +30,26 @@ const MyInput = forwardRef(function MyInput(props, ref) { }); ``` -[See more examples below.](#usage) +[Veja mais exemplos abaixo.](#usage) -#### Parameters {/*parameters*/} +#### Parâmetros {/*parameters*/} -* `render`: The render function for your component. React calls this function with the props and `ref` that your component received from its parent. The JSX you return will be the output of your component. +* `render`: A função de renderização do seu componente. O React usa essa função com as props e o `ref` que seu componente recebeu do pai. O JSX que irá retornar será o resultado do seu componente. -#### Returns {/*returns*/} +#### Retornos {/*returns*/} -`forwardRef` returns a React component that you can render in JSX. Unlike React components defined as plain functions, a component returned by `forwardRef` is also able to receive a `ref` prop. +`forwardRef` retorna um componente React que você pode renderizar em JSX. Diferente dos componentes React definidos como funções simples, um componente retornado por `forwardRef` também pode receber uma prop `ref`. -#### Caveats {/*caveats*/} +#### Advertências {/*caveats*/} -* In Strict Mode, React will **call your render function twice** in order to [help you find accidental impurities.](/reference/react/useState#my-initializer-or-updater-function-runs-twice) This is development-only behavior and does not affect production. If your render function is pure (as it should be), this should not affect the logic of your component. The result from one of the calls will be ignored. +* No Modo Estrito, o React **chamará sua função de renderização duas vezes** para [ajudar a encontrar impurezas acidentais.](/reference/react/useState#my-initializer-or-updater-function-runs-twice) Esse comportamento ocorre apenas no ambiente de desenvolvimento e não afeta a produção. Se sua função de renderização for pura (como deve ser), isso não afetará a lógica do seu componente. O resultado de uma das chamadas será ignorado. --- -### `render` function {/*render-function*/} - -`forwardRef` accepts a render function as an argument. React calls this function with `props` and `ref`: +### Função de `renderização` {/*render-function*/} +`forwardRef` aceita uma função de renderização como argumento. O React chama essa função com as props e o ref: ```js const MyInput = forwardRef(function MyInput(props, ref) { return ( @@ -64,21 +63,22 @@ const MyInput = forwardRef(function MyInput(props, ref) { #### Parameters {/*render-parameters*/} -* `props`: The props passed by the parent component. +* `props`: As props passadas pelo componente pai. -* `ref`: The `ref` attribute passed by the parent component. The `ref` can be an object or a function. If the parent component has not passed a ref, it will be `null`. You should either pass the `ref` you receive to another component, or pass it to [`useImperativeHandle`.](/reference/react/useImperativeHandle) +* `ref`: O atributo `ref` transmitido pelo componente pai. O `ref` pode ser um objeto ou uma função. Se o componente pai não passar um `ref`, ele será `null`. Você deve passar o `ref` recebido para outro componente ou usá-lo com [useImperativeHandle.le`.](/reference/react/useImperativeHandle) -#### Returns {/*render-returns*/} +#### Retornos {/*render-returns*/} -`forwardRef` returns a React component that you can render in JSX. Unlike React components defined as plain functions, the component returned by `forwardRef` is able to take a `ref` prop. +`forwardRef` retorna um componente React que você pode renderizar em JSX. Diferente dos componentes React definidos como funções simples, um componente retornado por `forwardRef` também pode receber uma prop `ref`. --- -## Usage {/*usage*/} +## Uso {/*usage*/} -### Exposing a DOM node to the parent component {/*exposing-a-dom-node-to-the-parent-component*/} +### Expondo um nó DOM ao componente pai {/*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 padrão, os nós DOM de cada componente são privados. No entanto, às vees é útil expor um nó DOM ao componente pai — por exemplo, para permitir que ele seja focado. Para optar +por participar, envolva sua definição de componente em `forwardRef()`: ```js {3,11} import { forwardRef } from 'react'; @@ -93,8 +93,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: +Você receberá um ref como segundo argumento após as props. Passe-o para o nó DOM que você deseja expor: ```js {8} [[1, 3, "ref"], [1, 8, "ref", 30]] import { forwardRef } from 'react'; @@ -109,8 +108,7 @@ const MyInput = forwardRef(function MyInput(props, ref) { ); }); ``` - -This lets the parent `Form` component access the `` DOM node exposed by `MyInput`: +Isso vai permitir que o componente pai `Form` acesse o `` DOM node exposto por `MyInput`: ```js [[1, 2, "ref"], [1, 10, "ref", 41], [2, 5, "ref.current"]] function Form() { @@ -131,15 +129,16 @@ function Form() { } ``` -This `Form` component [passes a ref](/reference/react/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. +Este componente `Form` [passa uma ref](/reference/react/useRef#manipulating-the-dom-with-a-ref) para `MyInput`. O componente `MyInput` encaminha essa `ref` para a tag `` do navegador. Como resultado, o componente Form pode acessar esse nó DOM `` e chamar [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) nele. + +Tenha em mente que expor uma referência ao nó DOM dentro do seu componente torna mais difícil alterar os internos do seu componente mais tarde. Você normalmente exporá nós DOM de componentes reutilizáveis de baixo nível, como botões ou entradas de texto, mas não fará isso para componentes de nível de aplicativo, como um avatar ou um comentário. -Keep in mind that exposing a ref to the DOM node inside your component makes 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. + - -#### Focusing a text input {/*focusing-a-text-input*/} +#### Focando uma 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 forwards that ref to the browser ``. This lets the `Form` component focus the ``. +Clicar no botão focará o `input`. O componente `Form` define uma ref e o passa para o componente `MyInput`. O componente `MyInput` encaminha esse ref para o navegador ``. Isso permite que o componente `Form` foque no ``. @@ -191,9 +190,9 @@ input { -#### Playing and pausing a video {/*playing-and-pausing-a-video*/} +#### Reproduzir e pausar um 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 `