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
Copy file name to clipboardExpand all lines: src/content/reference/react/useActionState.md
+51-51
Original file line number
Diff line number
Diff line change
@@ -5,19 +5,19 @@ canary: true
5
5
6
6
<Canary>
7
7
8
-
The `useActionState`Hook is currently only available in React's Canary and experimental channels. Learn more about [release channels here](/community/versioning-policy#all-release-channels). In addition, you need to use a framework that supports[React Server Components](/reference/rsc/use-client)to get the full benefit of`useActionState`.
8
+
O Hook `useActionState`está atualmente disponível apenas nos canais Canary e experimentais do React. Saiba mais sobre [canais de lançamento aqui](/community/versioning-policy#all-release-channels). Além disso, você precisa usar um framework que suporte[React Server Components](/reference/rsc/use-client)para obter o máximo benefício do`useActionState`.
9
9
10
10
</Canary>
11
11
12
12
<Note>
13
13
14
-
In earlier React Canary versions, this API was part of React DOM and called`useFormState`.
14
+
Nas versões anteriores do React Canary, esta API fazia parte do React DOM e era chamada de`useFormState`.
15
15
16
16
</Note>
17
17
18
18
<Intro>
19
19
20
-
`useActionState`is a Hook that allows you to update state based on the result of a form action.
20
+
`useActionState`é um Hook que permite atualizar o estado com base no resultado de uma ação de formulário.
{/* TODO T164397693: link to actions documentation once it exists */}
36
+
{/* TODO T164397693: link para a documentação de ações assim que ela existir */}
37
37
38
-
Call`useActionState`at the top level of your component to create component state that is updated [when a form action is invoked](/reference/react-dom/components/form). You pass `useActionState`an existing form action function as well as an initial state, and it returns a new action that you use in your form, along with the latest form state. The latest form state is also passed to the function that you provided.
38
+
Chame`useActionState`no nível superior do seu componente para criar um estado de componente que é atualizado [quando uma ação de formulário é invocada](/reference/react-dom/components/form). Você passa para `useActionState`uma função de ação de formulário existente, bem como um estado inicial, e ele retorna uma nova ação que você usa em seu formulário, juntamente com o estado mais recente do formulário. O estado mais recente do formulário também é passado para a função que você forneceu.
The form state is the value returned by the action when the form was last submitted. If the form has not yet been submitted, it is the initial state that you pass.
58
+
O estado do formulário é o valor retornado pela ação quando o formulário foi submetido pela última vez. Se o formulário ainda não foi submetido, é o estado inicial que você passa.
59
59
60
-
If used with a Server Action, `useActionState`allows the server's response from submitting the form to be shown even before hydration has completed.
60
+
Se usado com uma Ação do Servidor, `useActionState`permite que a resposta do servidor da submissão do formulário seja exibida mesmo antes que a hidratação tenha sido concluída.
61
61
62
-
[See more examples below.](#usage)
62
+
[Veja mais exemplos abaixo.](#usage)
63
63
64
-
#### Parameters {/*parameters*/}
64
+
#### Parâmetros {/*parameters*/}
65
65
66
-
* `fn`: The function to be called when the form is submitted or button pressed. When the function is called, it will receive the previous state of the form (initially the`initialState`that you pass, subsequently its previous return value) as its initial argument, followed by the arguments that a form action normally receives.
67
-
* `initialState`: The value you want the state to be initially. It can be any serializable value. This argument is ignored after the action is first invoked.
68
-
* **optional** `permalink`: A string containing the unique page URL that this form modifies. For use on pages with dynamic content (eg: feeds) in conjunction with progressive enhancement: if`fn`is a [server action](/reference/rsc/use-server) and the form is submitted before the JavaScript bundle loads, the browser will navigate to the specified permalink URL, rather than the current page's URL. Ensure that the same form component is rendered on the destination page (including the same action`fn`and`permalink`) so that React knows how to pass the state through. Once the form has been hydrated, this parameter has no effect.
66
+
* `fn`: A função a ser chamada quando o formulário é submetido ou o botão pressionado. Quando a função é chamada, ela receberá o estado anterior do formulário (inicialmente o`initialState`que você passa, posteriormente seu último valor de retorno) como seu argumento inicial, seguido pelos argumentos que uma ação de formulário normalmente recebe.
67
+
* `initialState`: O valor que você deseja que o estado tenha inicialmente. Ele pode ser qualquer valor serializável. Este argumento é ignorado após a ação ser invocada pela primeira vez.
68
+
* **opcional** `permalink`: Uma string contendo a URL única da página que este formulário modifica. Para uso em páginas com conteúdo dinâmico (por exemplo: feeds) em conjunto com aprimoramento progressivo: se`fn`for uma [ação do servidor](/reference/rsc/use-server) e o formulário for submetido antes que o pacote JavaScript carregue, o navegador navegará para a URL permalink especificada, em vez da URL da página atual. Certifique-se de que o mesmo componente de formulário seja renderizado na página de destino (incluindo a mesma ação`fn`e`permalink`) para que o React saiba como passar o estado. Uma vez que o formulário tenha sido hidratado, este parâmetro não tem efeito.
69
69
70
-
{/* TODO T164397693: link to serializable values docs once it exists */}
70
+
{/* TODO T164397693: link para a documentação de valores serializáveis assim que existir */}
71
71
72
-
#### Returns {/*returns*/}
72
+
#### Retorna {/*returns*/}
73
73
74
-
`useActionState`returns an array with exactly two values:
74
+
`useActionState`retorna um array com exatamente dois valores:
75
75
76
-
1. The current state. During the first render, it will match the `initialState`you have passed. After the action is invoked, it will match the value returned by the action.
77
-
2. A new action that you can pass as the `action`prop to your`form`component or`formAction`prop to any`button`component within the form.
76
+
1. O estado atual. Durante a primeira renderização, ele corresponderá ao `initialState`que você passou. Após a invocação da ação, ele corresponderá ao valor retornado pela ação.
77
+
2. Uma nova ação que você pode passar como a prop `action`para seu componente`form`ou prop`formAction`para qualquer componente`button`dentro do formulário.
78
78
79
-
#### Caveats {/*caveats*/}
79
+
#### Ressalvas {/*caveats*/}
80
80
81
-
* When used with a framework that supports React Server Components, `useActionState`lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state.
82
-
* The function passed to`useActionState`receives an extra argument, the previous or initial state, as its first argument. This makes its signature different than if it were used directly as a form action without using`useActionState`.
81
+
* Quando usado com um framework que suporta React Server Components, `useActionState`permite que você torne os formulários interativos antes que o JavaScript tenha sido executado no cliente. Quando usado sem Server Components, é equivalente ao estado local do componente.
82
+
* A função passada para`useActionState`recebe um argumento extra, o estado anterior ou inicial, como seu primeiro argumento. Isso torna sua assinatura diferente do que se ela fosse usada diretamente como uma ação de formulário sem usar`useActionState`.
83
83
84
84
---
85
85
86
-
## Usage {/*usage*/}
86
+
## Uso {/*usage*/}
87
87
88
-
### Using information returned by a form action {/*using-information-returned-by-a-form-action*/}
88
+
### Usando informações retornadas por uma ação de formulário {/*using-information-returned-by-a-form-action*/}
89
89
90
-
Call`useActionState`at the top level of your component to access the return value of an action from the last time a form was submitted.
90
+
Chame`useActionState`no nível superior do seu componente para acessar o valor retornado de uma ação da última vez que um formulário foi submetido.
`useActionState`returns an array with exactly two items:
107
+
`useActionState`retorna um array com exatamente dois itens:
108
108
109
-
1. The <CodeStep step={1}>current state</CodeStep> of the form, which is initially set to the <CodeStep step={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStep step={3}>action</CodeStep> you provided.
110
-
2. A <CodeStep step={2}>new action</CodeStep> that you pass to`<form>`as its `action` prop.
109
+
1. O <CodeStep step={1}>estado atual</CodeStep> do formulário, que é inicialmente definido para o <CodeStep step={4}>estado inicial</CodeStep> que você forneceu, e após a submissão do formulário é definido para o valor de retorno da <CodeStep step={3}>ação</CodeStep> que você forneceu.
110
+
2. Uma <CodeStep step={2}>nova ação</CodeStep> que você passa para`<form>`como sua prop `action`.
111
111
112
-
When the form is submitted, the <CodeStep step={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStep step={1}>current state</CodeStep> of the form.
112
+
Quando o formulário é submetido, a função <CodeStep step={3}>ação</CodeStep> que você forneceu será chamada. Seu valor de retorno se tornará o novo <CodeStep step={1}>estado atual</CodeStep> do formulário.
113
113
114
-
The <CodeStep step={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStep step={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStep step={4}>initial state</CodeStep> you provided, while with subsequent submissions, it will be the return value from the last time the action was called. The rest of the arguments are the same as if`useActionState`had not been used.
114
+
A <CodeStep step={3}>ação</CodeStep> que você fornece também receberá um novo primeiro argumento, que é o <CodeStep step={1}>estado atual</CodeStep> do formulário. A primeira vez que o formulário é submetido, isso será o <CodeStep step={4}>estado inicial</CodeStep> que você forneceu, enquanto em submissões subsequentes, será o valor de retorno da última vez em que a ação foi chamada. O restante dos argumentos é o mesmo do que se`useActionState`não tivesse sido usado.
115
115
116
116
```js [[3, 1, "action"], [1, 1, "currentState"]]
117
117
functionaction(currentState, formData) {
118
118
// ...
119
-
return'next state';
119
+
return'próximo estado';
120
120
}
121
121
```
122
122
123
-
<Recipes titleText="Display information after submitting a form" titleId="display-information-after-submitting-a-form">
123
+
<Recipes titleText="Exibir informações após submeter um formulário" titleId="display-information-after-submitting-a-form">
124
124
125
-
#### Display form errors {/*display-form-errors*/}
125
+
#### Exibir erros de formulário {/*display-form-errors*/}
126
126
127
-
To display messages such as an error message or toast that's returned by a Server Action, wrap the action in a call to`useActionState`.
127
+
Para exibir mensagens como uma mensagem de erro ou um toast que é retornado por uma Ação do Servidor, envolva a ação em uma chamada para`useActionState`.
128
128
129
129
<Sandpack>
130
130
@@ -138,7 +138,7 @@ function AddToCartForm({itemID, itemTitle}) {
return"Couldn't add to cart: the item is sold out.";
165
+
return"Não foi possível adicionar ao carrinho: o item está esgotado.";
166
166
}
167
167
}
168
168
```
@@ -194,9 +194,9 @@ form button {
194
194
195
195
<Solution />
196
196
197
-
#### Display structured information after submitting a form {/*display-structured-information-after-submitting-a-form*/}
197
+
#### Exibir informações estruturadas após submeter um formulário {/*display-structured-information-after-submitting-a-form*/}
198
198
199
-
The return value from a Server Action can be any serializable value. For example, it could be an object that includes a boolean indicating whether the action was successful, an error message, or updated information.
199
+
O valor de retorno de uma Ação do Servidor pode ser qualquer valor serializável. Por exemplo, pode ser um objeto que inclui um booleano indicando se a ação foi bem-sucedida, uma mensagem de erro ou informações atualizadas.
200
200
201
201
<Sandpack>
202
202
@@ -210,15 +210,15 @@ function AddToCartForm({itemID, itemTitle}) {
<button type="submit">Adicionar ao Carrinho</button>
214
214
{formState?.success&&
215
215
<div className="toast">
216
-
Added to cart! Your cart now has {formState.cartSize} items.
216
+
Adicionado ao carrinho! Seu carrinho agora tem {formState.cartSize} itens.
217
217
</div>
218
218
}
219
219
{formState?.success===false&&
220
220
<div className="error">
221
-
Failed to add to cart: {formState.message}
221
+
Falha ao adicionar ao carrinho: {formState.message}
222
222
</div>
223
223
}
224
224
</form>
@@ -228,8 +228,8 @@ function AddToCartForm({itemID, itemTitle}) {
228
228
exportdefaultfunctionApp() {
229
229
return (
230
230
<>
231
-
<AddToCartForm itemID="1" itemTitle="JavaScript: The Definitive Guide"/>
232
-
<AddToCartForm itemID="2" itemTitle="JavaScript: The Good Parts"/>
231
+
<AddToCartForm itemID="1" itemTitle="JavaScript: O Guia Definitivo"/>
232
+
<AddToCartForm itemID="2" itemTitle="JavaScript: Os Bons Partes"/>
233
233
</>
234
234
)
235
235
}
@@ -248,7 +248,7 @@ export async function addToCart(prevState, queryData) {
248
248
} else {
249
249
return {
250
250
success:false,
251
-
message:"The item is sold out.",
251
+
message:"O item está esgotado.",
252
252
};
253
253
}
254
254
}
@@ -283,14 +283,14 @@ form button {
283
283
284
284
</Recipes>
285
285
286
-
## Troubleshooting {/*troubleshooting*/}
286
+
## Solução de Problemas {/*troubleshooting*/}
287
287
288
-
### My action can no longer read the submitted form data {/*my-action-can-no-longer-read-the-submitted-form-data*/}
288
+
### Minha ação não consegue mais ler os dados do formulário submetido {/*my-action-can-no-longer-read-the-submitted-form-data*/}
289
289
290
-
When you wrap an action with`useActionState`, it gets an extra argument *as its first argument*. The submitted form data is therefore its *second* argument instead of its first as it would usually be. The new first argument that gets added is the current state of the form.
290
+
Quando você envolve uma ação com`useActionState`, ela recebe um argumento extra *como seu primeiro argumento*. Os dados do formulário submetido são, portanto, seu *segundo* argumento em vez de seu primeiro, como geralmente seria. O novo primeiro argumento que é adicionado é o estado atual do formulário.
0 commit comments