Skip to content

Commit 40a9421

Browse files
OlegDev1MichaelDeBoeybrookslybrand
authored
docs(components/form): add Registration Form example (#12123)
* docs(components/form): add Registration Form example * Apply suggestions from code review Co-authored-by: Michaël De Boey <[email protected]> * add missing links * change labelFor to htmlFor * Fix link --------- Co-authored-by: Michaël De Boey <[email protected]> Co-authored-by: Brooks Lybrand <[email protected]>
1 parent 5f34a96 commit 40a9421

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

docs/components/form.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,66 @@ You can access those values from the `request.url`
373373

374374
- [useSubmit][usesubmit]
375375

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+
<Form method="post">
386+
<label htmlFor="name">Name</label>
387+
<input type="text" name="name" />
388+
389+
<label htmlFor="email">Email</label>
390+
<input type="email" name="email" />
391+
392+
<label htmlFor="password">Password</label>
393+
<input type="password" name="password" />
394+
395+
<button type="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 = await request.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+
await fakePostRequest(postData);
421+
return redirect("/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.
430+
431+
**See also:**
432+
433+
- [`useActionData`][useactiondata]
434+
- [`redirect`][redirect]
435+
376436
[usenavigation]: ../hooks/use-navigation
377437
[useactiondata]: ../hooks/use-action-data
378438
[formdata]: https://developer.mozilla.org/en-US/docs/Web/API/FormData
@@ -395,3 +455,7 @@ You can access those values from the `request.url`
395455
[use-view-transition-state]: ../hooks//use-view-transition-state
396456
[view-transitions]: https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
397457
[relativesplatpath]: ../hooks/use-resolved-path#splat-paths
458+
[redirect]: ../fetch/redirect
459+
[action]: ../route/action
460+
[request-formData-method]: ../guides/form-data
461+
[mutation-submissions]: #mutation-submissions

0 commit comments

Comments
 (0)