Form Submission with Promise Feedback
Provide clear asynchronous status feedback during form validation, serialization, and network dispatch.
Combine form submission states with toast.promise() to give users immediate visual assurance during database mutations.
import React, { useState } from 'react';export function ContactForm() { const [submitting, setSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent
try {
await toast.promise(
fetch('/api/contact', { method: 'POST', body: formData }).then((r) => {
if (!r.ok) throw new Error('Submission failed');
return r.json();
}),
{
loading: 'Sending message...',
success: 'Message delivered! We will reply within 24 hours.',
error: (err) => Failed: ${(err as Error).message},
description: 'Secure TLS dispatch to customer support.',
}
).unwrap();
e.currentTarget.reset();
} finally {
setSubmitting(false);
}
};
return (
); }`Want to test these options live?
Launch the interactive playground to experiment with themes, positions, and live generators.