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-dom/hooks/useFormState.md
+42-42Lines changed: 42 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@ canary: true
5
5
6
6
<Canary>
7
7
8
-
The `useFormState`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/react/use-client)to get the full benefit of`useFormState`.
8
+
Le Hook `useFormState`n'est actuellement disponible que sur les canaux de livraison Canary et Expérimental de React. Apprenez-en davantage sur [les canaux de livraison React](/community/versioning-policy#all-release-channels). Par ailleurs, vous aurez besoin d'utiliser un framework qui prenne en charge les [React Server Components](/reference/react/use-client)pour tirer pleinement parti de`useFormState`.
9
9
10
10
</Canary>
11
11
12
12
<Intro>
13
13
14
-
`useFormState`is a Hook that allows you to update state based on the result of a form action.
14
+
`useFormState`est un Hook qui vous permet de mettre à jour l'état sur base du résultat d'une action de formulaire.
{/* TODO T164397693: link to actions documentation once it exists */}
31
31
32
-
Call`useFormState`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 `useFormState`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.
32
+
Appelez`useFormState`au niveau racine de votre composant pour créer un état de composant qui sera mis à jour [lorsqu'une action de formulaire sera invoquée](/reference/react-dom/components/form). Vous passez à `useFormState`une fonction d'action de formulaire existante ainsi qu'un état initial, et il renvoie une nouvelle action que vous pouvez utiliser dans votre formulaire, ainsi que le dernier état en date pour ce formulaire. Cet état sera également passé à la fonction que vous avez fournie.
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.
52
+
L'état de formulaire est déterminé par la valeur renvoyée par l'action lors du dernier envoi en date du formulaire. Si le formulaire n'a pas encore été envoyé, il équivaut à l'état initial que vous avez fourni.
53
53
54
-
If used with a server action, `useFormState`allows the server's response from submitting the form to be shown even before hydration has completed.
54
+
Lorsque vous l'utilisez dans une action serveur, `useFormState`permet d'afficher la réponse du serveur pour l'envoi du formulaire avant même que l'hydratation ne soit terminée.
55
55
56
-
[See more examples below.](#usage)
56
+
[Voir d'autres exemples plus bas](#usage).
57
57
58
-
#### Parameters {/*parameters*/}
58
+
#### Paramètres {/*parameters*/}
59
59
60
-
*`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.
61
-
*`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.
60
+
*`fn` : la fonction à appeler lorsque le formulaire est envoyé. Lorsque la fonction est appelée, elle reçoit comme premier argument l'état précédent du formulaire (le `initialState`que vous avez fourni pour le premier appel, puis, pour les appels ultérieurs, la valeur précédemment renvoyée), suivi par les arguments normalement acceptés par une fonction d'action de formulaire.
61
+
*`initialState` : la valeur initiale que vous souhaitez pour votre état. Il peut s'agir de n'importe quelle valeur sérialisable. Cet argument est ignoré après l'appel initial de l'action.
62
62
63
63
{/* TODO T164397693: link to serializable values docs once it exists */}
64
64
65
-
#### Returns {/*returns*/}
65
+
#### Valeur renvoyée {/*returns*/}
66
66
67
-
`useFormState`returns an array with exactly two values:
67
+
`useFormState`renvoie un tableau avec exactement deux valeurs :
68
68
69
-
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.
70
-
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.
69
+
1.L'état courant. Lors du rendu initial, il s'agira du `initialState`que vous avez passé. Après que l'action aura été invoquée, il correspondra à la valeur renvoyée par l'action.
70
+
2.Une nouvelle action que vous pouvez passer comme prop`action`à votre composant`form`, ou comme prop `formAction`à tout composant`button`au sein du formulaire.
71
71
72
-
#### Caveats {/*caveats*/}
72
+
#### Limitations {/*caveats*/}
73
73
74
-
*When used with a framework that supports React Server Components, `useFormState`lets you make forms interactive before JavaScript has executed on the client. When used without Server Components, it is equivalent to component local state.
75
-
*The function passed to`useFormState`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`useFormState`.
74
+
*Lorsque vous utilisez un framework prenant en charge les React Server Components, `useFormState`vous permet de rendre les formulaires interactifs avant même que JavaScript ne soit exécuté côté client. Si vous l'utilisez hors des Server Components, il est équivalent à un état local de composant.
75
+
*La fonction passée à`useFormState`reçoit un premier argument supplémentaire : l'état précédent ou initial. Sa signature est donc différente de celles des fonctions d'action de formulaire utilisées directement par vos formulaires, sans recours à`useFormState`.
76
76
77
77
---
78
78
79
-
## Usage {/*usage*/}
79
+
## Utilisation {/*usage*/}
80
80
81
-
### Using information returned by a form action {/*using-information-returned-by-a-form-action*/}
81
+
### Utiliser les informations renvoyées par une action de formulaire {/*using-information-returned-by-a-form-action*/}
82
82
83
-
Call`useFormState`at the top level of your component to access the return value of an action from the last time a form was submitted.
83
+
Appelez`useFormState`au niveau racine de votre composant pour accéder à la valeur renvoyée par l'action lors du dernier envoi du formulaire.
`useFormState`returns an array with exactly two items:
100
+
`useFormState`renvoie un tableau avec exactement deux valeurs :
101
101
102
-
1.The <CodeStepstep={1}>current state</CodeStep> of the form, which is initially set to the <CodeStepstep={4}>initial state</CodeStep> you provided, and after the form is submitted is set to the return value of the <CodeStepstep={3}>action</CodeStep> you provided.
103
-
2.A <CodeStepstep={2}>new action</CodeStep> that you pass to `<form>` as its `action` prop.
102
+
1. <CodeStepstep={1}>L'état courant</CodeStep> du formulaire, qui sera au départ <CodeStepstep={4}>l'état initial</CodeStep> que vous aurez fourni, puis une fois le formulaire envoyé, vaudra la valeur de retour de <CodeStepstep={3}>l'action</CodeStep> que vous aurez passée.
103
+
2.Une <CodeStepstep={2}>nouvelle action</CodeStep> que vous pouvez passer comme prop `action` à votre `form`.
104
104
105
-
When the form is submitted, the <CodeStepstep={3}>action</CodeStep> function that you provided will be called. Its return value will become the new <CodeStepstep={1}>current state</CodeStep> of the form.
105
+
Lorsque le formulaire sera envoyé, la fonction <CodeStepstep={3}>d'action</CodeStep> que vous aurez fournie sera appelée. Sa valeur de retour deviendra le nouvel <CodeStepstep={1}>état courant</CodeStep> du formulaire.
106
106
107
-
The <CodeStepstep={3}>action</CodeStep> that you provide will also receive a new first argument, namely the <CodeStepstep={1}>current state</CodeStep> of the form. The first time the form is submitted, this will be the <CodeStepstep={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`useFormState` had not been used
107
+
<CodeStepstep={3}>L'action</CodeStep> que vous aurez fournie recevra par ailleurs un premier argument supplémentaire, à savoir <CodeStepstep={1}>l'état courant</CodeStep> du formulaire. Lors du premier envoi du formulaire, il s'agira de <CodeStepstep={4}>l'état initial</CodeStep> que vous aurez fourni, alors que pour les envois ultérieurs, ce sera la valeur renvoyée par le dernier appel en date de l'action. Le reste des arguments est identique à une utilisation de l'action sans`useFormState`.
108
108
109
109
```js [[3, 1, "action"], [1, 1, "currentState"]]
110
110
functionaction(currentState, formData) {
111
111
// ...
112
-
return'next state';
112
+
return'prochain état';
113
113
}
114
114
```
115
115
116
-
<RecipestitleText="Display information after submitting a form"titleId="display-information-after-submitting-a-form">
116
+
<RecipestitleText="Afficher des informations suite à l’envoi d'un formulaire"titleId="display-information-after-submitting-a-form">
117
117
118
-
#### Display form errors {/*display-form-errors*/}
118
+
#### Afficher des erreurs de formulaire {/*display-form-errors*/}
119
119
120
-
To display messages such as an error message or toast that's returned by a server action, wrap the action in a call to`useFormState`.
120
+
Pour afficher des messages tels qu'un message d'erreur ou une notification renvoyés par une action serveur, enrobez l'action dans un appel à`useFormState`.
121
121
122
122
<Sandpack>
123
123
@@ -132,7 +132,7 @@ function AddToCartForm({itemID, itemTitle}) {
return"Couldn't add to cart: the item is sold out.";
159
+
return"Impossible d’ajouter au panier : ce titre est épuisé.";
160
160
}
161
161
}
162
162
```
@@ -188,9 +188,9 @@ form button {
188
188
189
189
<Solution />
190
190
191
-
#### Display structured information after submitting a form {/*display-structured-information-after-submitting-a-form*/}
191
+
#### Afficher des données structurées suite à l'envoi d'un formulaire {/*display-structured-information-after-submitting-a-form*/}
192
192
193
-
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.
193
+
La valeur renvoyée par une action serveur peut être n'importe quelle valeur sérialisable. Il peut par exemple s'agir d'un objet avec un booléen indiquant si l'action a réussi, un message d'erreur, ou des informations mises à jour.
194
194
195
195
<Sandpack>
196
196
@@ -205,15 +205,15 @@ function AddToCartForm({itemID, itemTitle}) {
@@ -243,7 +243,7 @@ export async function addToCart(prevState, queryData) {
243
243
} else {
244
244
return {
245
245
success:false,
246
-
message:"The item is sold out.",
246
+
message:"Ce titre est épuisé.",
247
247
};
248
248
}
249
249
}
@@ -278,11 +278,11 @@ form button {
278
278
279
279
</Recipes>
280
280
281
-
## Troubleshooting {/*troubleshooting*/}
281
+
## Dépannage {/*troubleshooting*/}
282
282
283
-
### My action can no longer read the submitted form data {/*my-action-can-no-longer-read-the-submitted-form-data*/}
283
+
### Mon action n'arrive plus à lire les données envoyées par le formulaire {/*my-action-can-no-longer-read-the-submitted-form-data*/}
284
284
285
-
When you wrap an action with`useFormState`, 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.
285
+
Lorsque vous enrobez une action avec`useFormState`, elle reçoit un *premier argument* supplémentaire. Les données envoyées par le formulaire sont donc présentes comme *second* argument, plutôt qu'en première position comme en temps normal. Le premier argument ainsi injecté contient l'état actuel du formulaire.
0 commit comments