@@ -372,28 +372,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}.
}
>
);
@@ -410,17 +410,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 uma entrada passando algum `value` para ela, você a *força* a sempre ter o valor que você passou. Portanto, se você passar uma variável de estado como um `value`, mas esquecer de atualizar essa variável de estado de forma síncrona durante o manipulador de eventos `onChange`, o React reverterá a entrada após cada pressionamento de tecla de volta para o `value` que você especificou.
---
-### Optimizing re-rendering on every keystroke {/*optimizing-re-rendering-on-every-keystroke*/}
+### Otimizando a renderização a cada pressionamento de tecla {/*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 uma entrada controlada, você define o estado a cada pressionamento de tecla. Se o componente que contém seu estado renderizar uma árvore grande, isso pode ficar lento. Existem algumas maneiras de otimizar o desempenho da 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 renderiza todo o conteúdo da página a cada pressionamento de tecla:
```js {5-8}
function App() {
@@ -436,7 +436,7 @@ function App() {
}
```
-Since ` ` doesn't rely on the input state, you can move the input state into its own component:
+Como o ` ` 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() {
@@ -458,140 +458,139 @@ function SignupForm() {
}
```
-This significantly improves performance because now only `SignupForm` re-renders on every keystroke.
+Isso melhora significativamente o desempenho porque agora somente `SignupForm` renderiza a cada pressionamento de tecla.
-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 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 a entrada controlada responsiva mesmo no meio de uma grande 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 uma entrada 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` para um campo de formulário sem um manipulador `onChange`. Isso renderizará um campo somente leitura. Se o campo deve 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ê só queria [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ê quer [controlar esta entrada 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 alteração
```
---
-### My checkbox doesn't update when I click on it {/*my-checkbox-doesnt-update-when-i-click-on-it*/}
+### Minha checkbox não atualiza quando clico nela {/*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 uma checkbox com `checked`, mas sem `onChange`, verá um erro no console:
```js
-// 🔴 Bug: controlled checkbox with no onChange handler
+// 🔴 Bug: checkbox controlada 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` para um campo de formulário sem um manipulador `onChange`. Isso renderizará um campo somente leitura. Se o campo deve 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ê só queria [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 controlada 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ê quer [controlar esta 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 controlada 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 a 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 alteração
```
---
-### My input caret jumps to the beginning on every keystroke {/*my-input-caret-jumps-to-the-beginning-on-every-keystroke*/}
+### Meu cursor de entrada pula para o início a cada pressionamento de tecla {/*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 uma entrada,](#controlling-an-input-with-a-state-variable) você deve atualizar sua variável de estado para o valor da entrada 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 uma entrada 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 uma entrada 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 de forma síncrona para `e.target.value`:
```js
function handleChange(e) {
- // ✅ Updating a controlled input to e.target.value synchronously
+ // ✅ Atualizando uma entrada controlada para e.target.value de forma síncrona
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 a entrada seja removida e readicionada do DOM a cada pressionamento de tecla. Isso pode acontecer se você estiver [redefinindo acidentalmente o estado](/learn/preserving-and-resetting-state) a cada renderização, por exemplo, se a entrada ou um de seus pais sempre receber um atributo `key` diferente, ou se você aninhar definições de função de componente (o que não é suportado e faz com que o componente "interno" seja sempre 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á alterando uma entrada não controlada para ser controlada" {/*im-getting-an-error-a-component-is-changing-an-uncontrolled-input-to-be-controlled*/}
+Se você fornecer um `value` para o componente, ele deve permanecer uma string durante 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="some string"` porque o React não saberá se você deseja que o componente seja não controlado ou controlado. Um componente controlado sempre deve 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, defina-o 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 uma checkbox, certifique-se de que seja sempre um booleano.
\ No newline at end of file