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: docs/components/form.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -373,6 +373,66 @@ You can access those values from the `request.url`
373
373
374
374
-[useSubmit][usesubmit]
375
375
376
+
## Registration Form
377
+
378
+
We can use [Mutation Submissions][mutation-submissions] in registration workflow.
379
+
380
+
```tsx
381
+
function RegisterForm() {
382
+
const actionData =useActionData();
383
+
384
+
return (
385
+
<Formmethod="post">
386
+
<labelhtmlFor="name">Name</label>
387
+
<inputtype="text"name="name" />
388
+
389
+
<labelhtmlFor="email">Email</label>
390
+
<inputtype="email"name="email" />
391
+
392
+
<labelhtmlFor="password">Password</label>
393
+
<inputtype="password"name="password" />
394
+
395
+
<buttontype="submit">Register</button>
396
+
397
+
{actionData?.error? (
398
+
<p>An error occured: {actionData.error}</p>
399
+
) :null}
400
+
</Form>
401
+
);
402
+
}
403
+
```
404
+
405
+
When the user submits the form by clicking "Register", the [`action`][action] function is executed. Note that we are accessing the returned value from the [`action`][action] function in our component using [`useActionData`][useactiondata].
406
+
407
+
```tsx
408
+
<Route
409
+
path="/register"
410
+
action={async ({ request }) => {
411
+
const data =awaitrequest.formData();
412
+
413
+
const postData = {
414
+
name: data.get("name"),
415
+
email: data.get("email"),
416
+
password: data.get("password"),
417
+
};
418
+
419
+
try {
420
+
awaitfakePostRequest(postData);
421
+
returnredirect("/dashboard");
422
+
} catch (e) {
423
+
return { error: "Cannot send the request" };
424
+
}
425
+
}}
426
+
/>
427
+
```
428
+
429
+
In the [`action`][action], we then access the form data (`name`, `email` and `password`) using the [`request.formData`][request-formData-method] method. After that, the `POST` request is sent to the server. If it is successful, the user will be redirected to `/dashboard` page. Otherwise, an error message will appear.
0 commit comments