handleSubmit calls onSubmit again while a submit is still pending (since 1.33.1)

Aperta Adatta ai principianti
#2,467 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react, typescript
Ambito
frontend

Direzione di ricerca

Il report indica _handleSubmit e this.baseStore.state.isSubmitting in @tanstack/form-core; inizia tracciando il ritorno anticipato e il controllo del tentativo di invio descritti nell’issue. Riproduci il problema con lo script Node fornito sul codice attuale, quindi aggiungi o aggiorna un test di regressione per un secondo invio mentre un onSubmit è ancora in corso; il lavoro è completato quando onSubmit viene chiamato una sola volta e il comportamento di nuovo invio di #2120 viene mantenuto.

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

Descrizione

Describe the bug

Since #2120 ("clear stale onBlur errors on re-submission", released in 1.33.1), handleSubmit skips its !canSubmit early return on every attempt after the first. canSubmit is also false while a submit is in flight, so a second handleSubmit during a pending onSubmit now runs validation and calls onSubmit again. Up to 1.33.0 that second call returned early.

In practice: a double tap on a submit button, or Enter / the keyboard's "go" pressed twice, sends the request twice. We hit it on a sign-in form and a comment composer in React Native, where the keyboard submit bypasses a disabled button.

Your minimal, reproducible example

No sandbox, it needs only @tanstack/form-core in Node:

// npm i @tanstack/[email protected], then: node repro.mjs
import { FormApi } from '@tanstack/form-core';

const pending = [];
const form = new FormApi({
  defaultValues: { name: 'Ada' },
  onSubmit: () => new Promise((resolve) => pending.push(resolve)),
});
form.mount();

const first = form.handleSubmit();
await new Promise((resolve) => setTimeout(resolve, 0));
console.log('isSubmitting:', form.state.isSubmitting, 'canSubmit:', form.state.canSubmit);

const second = form.handleSubmit();
await new Promise((resolve) => setTimeout(resolve, 0));

console.log('onSubmit calls:', pending.length, '(expected 1)');
pending.forEach((resolve) => resolve());
await Promise.all([first, second]);
Steps to reproduce
  1. Run the script with @tanstack/[email protected]: isSubmitting: true canSubmit: false, onSubmit calls: 1.
  2. Run it with @tanstack/[email protected]: same state, onSubmit calls: 2.
Expected behavior

A handleSubmit that arrives while isSubmitting is true does not call onSubmit again, as before #2120. Keeping the re-submission behaviour #2120 added and also returning early when this.baseStore.state.isSubmitting is true would cover both.

How often does this bug happen?

Every time

Platform
  • @tanstack/form-core 1.33.5 (via @tanstack/react-form 1.33.5)
  • Node 26, and React Native 0.88 / Hermes in the app
TanStack Form adapter

react-form

TanStack Form version

1.33.5

TypeScript version

5.9

Additional context

The change is in _handleSubmit: if (!this.state.canSubmit && ...) { if (this.baseStore.state.submissionAttempts <= 1) { ...; return; } }. We now wrap onSubmit in a single-flight guard on our side.

Lingua principale
TypeScript
Stelle
6.8k
Fork
705
Merge medio
1g 6h
PR unite (30g)
12

Preparare l'ambiente

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.

Altre issue di TanStack/form

Tutte le issue di TanStack/form

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.