reactjs / reactjs/react.dev

[Suggestion]: Provide examples for useActionState with onSubmit handlers

Aperta
#7,310 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type: documentation
Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla pagina di riferimento di useActionState collegata nell’issue e rivedi l’esempio esistente di form action. Aggiungi un esempio di onSubmit per moduli controllati e a più passaggi che spieghi come invocare la form action e gestirne il risultato, quindi verifica che il comportamento documentato chiarisca la confusione segnalata su await e TypeScript.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation, frontend
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.