reactjs / reactjs/react.dev

[Suggestion]: Provide examples for useActionState with onSubmit handlers

Offen
#7,310 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

type: documentation
Vorherrschende Sprache
JavaScript
Sterne
11.8k
Forks
7.9k
Ø Merge
1 T. 11 Std.
Gemergte PRs (30 T.)
11

Beschreibung

Summary

Add examples to the useActionState documentation showcasing how it can work inside onSubmit handlers of forms.

Page

https://react.dev/reference/react/useActionState

Details

The docs show how to use useActionState with the action property of a form. However, the docs don't show how to use useActionState when you need to use it from onSubmit instead. This has been something that I have been fighting.

Let's say you are developing a multi-step / wizard form across multiple components. Not all of the form is rendered at the same time, so uncontrolled components won't work here. You need to use controlled components for your input fields in order to keep all the form state as you go through steps.

When it comes to submission, using action={formAction} won't work anymore, because that will only submit with the input fields currently rendered. It is not at all clear what the expectations are for doing this in the onSubmit handler instead.

When I attempt to do something like this:

        <form
          onSubmit={form.onSubmit(async (values, event) => {
            const formData = new FormData();

            for (const [key, value] of Object.entries(values)) {
              formData.append(key, value);
            }

            startTransition(async () => {
              await formAction(formData);
            });
          })}
        >

I get interesting messages like TS80007: await has no effect on the type of this expression. on the line calling my form action, and I can't await the result and/or handle it.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der im Issue verlinkten useActionState-Referenzseite und sehen Sie sich das dort vorhandene Beispiel für eine Form action an. Fügen Sie ein onSubmit-Beispiel für kontrollierte, mehrstufige Formulare hinzu, das erklärt, wie die Form action aufgerufen und ihr Ergebnis verarbeitet wird. Überprüfen Sie anschließend, dass das dokumentierte Verhalten die gemeldeten Unklarheiten zu await und TypeScript behandelt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, react
Bereich
documentation, frontend
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.