@@ -378,28 +378,28 @@ export default function Form() {
return (
<>
- First name:
+ Primeiro nome:
setFirstName(e.target.value)}
/>
- Age:
+ Idade:
setAge(e.target.value)}
type="number"
/>
setAge(ageAsNumber + 10)}>
- Add 10 years
+ Adicionar 10 anos
{firstName !== '' &&
- Your name is {firstName}.
+ Seu nome é {firstName}.
}
{ageAsNumber > 0 &&
- Your age is {ageAsNumber}.
+ Sua idade é {ageAsNumber}.
}
>
);
@@ -416,17 +416,17 @@ p { font-weight: bold; }
-**If you pass `value` without `onChange`, it will be impossible to type into the input.** When you control an input by passing some `value` to it, you *force* it to always have the value you passed. So if you pass a state variable as a `value` but forget to update that state variable synchronously during the `onChange` event handler, React will revert the input after every keystroke back to the `value` that you specified.
+**Se você passar `value` sem `onChange`, será impossível digitar na entrada.** Quando você controla um input passando algum `value` para ele, você *força* ele a sempre ter o valor que você passou. Então, se você passar uma variável de estado como `value` mas esquecer de atualizar essa variável de estado sincronicamente durante o manipulador de eventos `onChange`, o React reverterá o input após cada tecla pressionada de volta para o `value` que você especificou.
---
-### Optimizing re-rendering on every keystroke {/*optimizing-re-rendering-on-every-keystroke*/}
+### Otimizando a re-renderização em cada tecla pressionada {/*optimizing-re-rendering-on-every-keystroke*/}
-When you use a controlled input, you set the state on every keystroke. If the component containing your state re-renders a large tree, this can get slow. There's a few ways you can optimize re-rendering performance.
+Quando você usa um input controlado, você define o estado em cada tecla pressionada. Se o componente que contém seu estado re-renderiza uma árvore grande, isso pode ficar lento. Existem algumas maneiras de otimizar o desempenho da re-renderização.
-For example, suppose you start with a form that re-renders all page content on every keystroke:
+Por exemplo, suponha que você comece com um formulário que re-renderiza todo o conteúdo da página a cada tecla pressionada:
```js {5-8}
function App() {
@@ -442,7 +442,7 @@ function App() {
}
```
-Since ` ` doesn't rely on the input state, you can move the input state into its own component:
+Como ` ` não depende do estado da entrada, você pode mover o estado da entrada para seu próprio componente:
```js {4,10-17}
function App() {
@@ -464,140 +464,139 @@ function SignupForm() {
}
```
-This significantly improves performance because now only `SignupForm` re-renders on every keystroke.
+Isso melhora significativamente o desempenho porque agora apenas o `SignupForm` re-renderiza a cada tecla pressionada.
-If there is no way to avoid re-rendering (for example, if `PageContent` depends on the search input's value), [`useDeferredValue`](/reference/react/useDeferredValue#deferring-re-rendering-for-a-part-of-the-ui) lets you keep the controlled input responsive even in the middle of a large re-render.
+Se não houver como evitar a re-renderização (por exemplo, se `PageContent` depender do valor da entrada de pesquisa), [`useDeferredValue`](/reference/react/useDeferredValue#deferring-re-rendering-for-a-part-of-the-ui) permite que você mantenha o input controlado responsivo mesmo no meio de uma grande re-renderização.
---
-## Troubleshooting {/*troubleshooting*/}
+## Solução de Problemas {/*troubleshooting*/}
-### My text input doesn't update when I type into it {/*my-text-input-doesnt-update-when-i-type-into-it*/}
+### Minha entrada de texto não atualiza quando digito nela {/*my-text-input-doesnt-update-when-i-type-into-it*/}
-If you render an input with `value` but no `onChange`, you will see an error in the console:
+Se você renderizar um input com `value` mas sem `onChange`, verá um erro no console:
```js
-// 🔴 Bug: controlled text input with no onChange handler
+// 🔴 Bug: entrada de texto controlada sem manipulador onChange
```
-You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.
+Você forneceu uma prop `value` a um campo de formulário sem um manipulador `onChange`. Isso renderizará um campo somente leitura. Se o campo deveria ser mutável, use `defaultValue`. Caso contrário, defina `onChange` ou `readOnly`.
-As the error message suggests, if you only wanted to [specify the *initial* value,](#providing-an-initial-value-for-an-input) pass `defaultValue` instead:
+Como a mensagem de erro sugere, se você apenas quisesse [especificar o *valor inicial,](#providing-an-initial-value-for-an-input) passe `defaultValue` em vez disso:
```js
-// ✅ Good: uncontrolled input with an initial value
+// ✅ Bom: entrada não controlada com um valor inicial
```
-If you want [to control this input with a state variable,](#controlling-an-input-with-a-state-variable) specify an `onChange` handler:
+Se você quiser [controlar esse input com uma variável de estado,](#controlling-an-input-with-a-state-variable) especifique um manipulador `onChange`:
```js
-// ✅ Good: controlled input with onChange
+// ✅ Bom: entrada controlada com onChange
setSomething(e.target.value)} />
```
-If the value is intentionally read-only, add a `readOnly` prop to suppress the error:
+Se o valor for intencionalmente somente leitura, adicione uma prop `readOnly` para suprimir o erro:
```js
-// ✅ Good: readonly controlled input without on change
+// ✅ Bom: entrada controlada somente leitura sem on change
```
---
-### My checkbox doesn't update when I click on it {/*my-checkbox-doesnt-update-when-i-click-on-it*/}
+### Meu checkbox não atualiza quando clico nele {/*my-checkbox-doesnt-update-when-i-click-on-it*/}
-If you render a checkbox with `checked` but no `onChange`, you will see an error in the console:
+Se você renderizar um checkbox com `checked` mas sem `onChange`, verá um erro no console:
```js
-// 🔴 Bug: controlled checkbox with no onChange handler
+// 🔴 Bug: checkbox controlado sem manipulador onChange
```
-You provided a `checked` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultChecked`. Otherwise, set either `onChange` or `readOnly`.
+Você forneceu uma prop `checked` a um campo de formulário sem um manipulador `onChange`. Isso renderizará um campo somente leitura. Se o campo deveria ser mutável, use `defaultChecked`. Caso contrário, defina `onChange` ou `readOnly`.
-As the error message suggests, if you only wanted to [specify the *initial* value,](#providing-an-initial-value-for-an-input) pass `defaultChecked` instead:
+Como a mensagem de erro sugere, se você apenas quisesse [especificar o *valor inicial,](#providing-an-initial-value-for-an-input) passe `defaultChecked` em vez disso:
```js
-// ✅ Good: uncontrolled checkbox with an initial value
+// ✅ Bom: checkbox não controlado com um valor inicial
```
-If you want [to control this checkbox with a state variable,](#controlling-an-input-with-a-state-variable) specify an `onChange` handler:
+Se você quiser [controlar esse checkbox com uma variável de estado,](#controlling-an-input-with-a-state-variable) especifique um manipulador `onChange`:
```js
-// ✅ Good: controlled checkbox with onChange
+// ✅ Bom: checkbox controlado com onChange
setSomething(e.target.checked)} />
```
-You need to read `e.target.checked` rather than `e.target.value` for checkboxes.
+Você precisa ler `e.target.checked` em vez de `e.target.value` para checkboxes.
-If the checkbox is intentionally read-only, add a `readOnly` prop to suppress the error:
+Se o checkbox for intencionalmente somente leitura, adicione uma prop `readOnly` para suprimir o erro:
```js
-// ✅ Good: readonly controlled input without on change
+// ✅ Bom: entrada controlada somente leitura sem on change
```
---
-### My input caret jumps to the beginning on every keystroke {/*my-input-caret-jumps-to-the-beginning-on-every-keystroke*/}
+### Meu cursor de entrada salta para o início a cada tecla pressionada {/*my-input-caret-jumps-to-the-beginning-on-every-keystroke*/}
-If you [control an input,](#controlling-an-input-with-a-state-variable) you must update its state variable to the input's value from the DOM during `onChange`.
+Se você [controla um input,](#controlling-an-input-with-a-state-variable) deve atualizar sua variável de estado para o valor do input do DOM durante `onChange`.
-You can't update it to something other than `e.target.value` (or `e.target.checked` for checkboxes):
+Você não pode atualizá-lo para algo diferente de `e.target.value` (ou `e.target.checked` para checkboxes):
```js
function handleChange(e) {
- // 🔴 Bug: updating an input to something other than e.target.value
+ // 🔴 Bug: atualizando um input para algo diferente de e.target.value
setFirstName(e.target.value.toUpperCase());
}
```
-You also can't update it asynchronously:
+Você também não pode atualizá-lo de forma assíncrona:
```js
function handleChange(e) {
- // 🔴 Bug: updating an input asynchronously
+ // 🔴 Bug: atualizando um input de forma assíncrona
setTimeout(() => {
setFirstName(e.target.value);
}, 100);
}
```
-To fix your code, update it synchronously to `e.target.value`:
+Para corrigir seu código, atualize-o sincronicamente para `e.target.value`:
```js
function handleChange(e) {
- // ✅ Updating a controlled input to e.target.value synchronously
+ // ✅ Atualizando um input controlado para e.target.value sincronicamente
setFirstName(e.target.value);
}
```
-If this doesn't fix the problem, it's possible that the input gets removed and re-added from the DOM on every keystroke. This can happen if you're accidentally [resetting state](/learn/preserving-and-resetting-state) on every re-render, for example if the input or one of its parents always receives a different `key` attribute, or if you nest component function definitions (which is not supported and causes the "inner" component to always be considered a different tree).
+Se isso não resolver o problema, é possível que o input esteja sendo removido e re-adicionado do DOM a cada tecla pressionada. Isso pode acontecer se você acidentalmente [resetar o estado](/learn/preserving-and-resetting-state) a cada re-renderização, por exemplo, se o input ou um de seus pais sempre recebe um atributo `key` diferente, ou se você aninhar definições de funções de componentes (o que não é suportado e faz com que o "componente interno" sempre seja considerado uma árvore diferente).
---
-### I'm getting an error: "A component is changing an uncontrolled input to be controlled" {/*im-getting-an-error-a-component-is-changing-an-uncontrolled-input-to-be-controlled*/}
+### Estou recebendo um erro: "Um componente está mudando um input não controlado para ser controlado" {/*im-getting-an-error-a-component-is-changing-an-uncontrolled-input-to-be-controlled*/}
+Se você fornecer um `value` ao componente, ele deve permanecer uma string durante toda a sua vida útil.
-If you provide a `value` to the component, it must remain a string throughout its lifetime.
+Você não pode passar `value={undefined}` primeiro e depois passar `value="alguma string"` porque o React não saberá se você deseja que o componente seja não controlado ou controlado. Um componente controlado deve sempre receber um `value` string, não `null` ou `undefined`.
-You cannot pass `value={undefined}` first and later pass `value="some string"` because React won't know whether you want the component to be uncontrolled or controlled. A controlled component should always receive a string `value`, not `null` or `undefined`.
+Se seu `value` estiver vindo de uma API ou de uma variável de estado, ele pode ser inicializado como `null` ou `undefined`. Nesse caso, ou o defina como uma string vazia (`''`) inicialmente, ou passe `value={someValue ?? ''}` para garantir que `value` seja uma string.
-If your `value` is coming from an API or a state variable, it might be initialized to `null` or `undefined`. In that case, either set it to an empty string (`''`) initially, or pass `value={someValue ?? ''}` to ensure `value` is a string.
-
-Similarly, if you pass `checked` to a checkbox, ensure it's always a boolean.
+Da mesma forma, se você passar `checked` para um checkbox, certifique-se de que ele seja sempre um booleano.
\ No newline at end of file